如何在注册表中设置一个值?

jun 2000-06-27 11:57:00
我想在software下建一个\mykey\Interface项,然后在其中加上值:IP 1.1.1.1,port 8080,我象下面这么写,但是有些参数不知道该怎么处理,总是Fail to create key,
着一步完了以后,下一步又该怎么做?

HKEY hKey;
PHKEY phkResult;
LPCTSTR SubKey = "\\Software\\myKey\\Interface\\";
LPTSTR regClass = "REG_SZ"; ?应该如何设置
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,SubKey,0,regClass,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,phkResult,(LPDWORD)REG_CREATED_NEW_KEY) != ERROR_SUCCESS)
{
AfxMessageBox("Failed to create key!");
}
...全文
266 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
PatrickGamp 2000-06-27
  • 打赏
  • 举报
回复
HKEY hKey;
LPCTSTR lpTitle="Software\\mykey\\Interface";
LPTSTR lpClassName="fkjg";//any
LPCTSTR lpName="IP";
LPTSTR lpValue="1,1,1,1,port 8080";
DWORD dwResult;

CreateKeyEx(HKEY_LOCAL_MACHINE,lpTitle,0,lpClassName,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwResult);
SetValueKeyEx(hKey,lpName,0,REG_SZ,(CONST BYTE *)lpValue,strlen(lpValue)+1);
RegCloseKey();
innerboy 2000-06-27
  • 打赏
  • 举报
回复
请看如下程序段,会又帮助的
BOOL W95StartService( DWORD dwType )
{
// Local Variables
TCHAR lpszBuff[256];
LPTSTR lpszStr = lpszBuff +128;
LPTSTR lpszName = lpszBuff;
//HANDLE hKey = NULL;
struct HKEY__ * hKey = NULL;
DWORD dwStrCb = 0;
DWORD dwValueType = 0;

// Get service name currently
lpszName = GetCommandLine();
for( int i = strlen(lpszName)-1; i>=0; i-- )
// for( int i = _tcslen(lpszName)-1; i>=0; i-- )
{
if( ( lpszName[i] != '"' )&&( lpszName[i]!=' ') )
break;
else if( lpszName[i] == '"' )
lpszName[i] = '\0';
}
if( lpszName[0] == '"' )
lpszName = lpszName +1;

// Registe as start up service
if( RegOpenKeyEx (HKEY_LOCAL_MACHINE,
TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunServices"),
0,
KEY_QUERY_VALUE | KEY_SET_VALUE,
&hKey ) != ERROR_SUCCESS )
{
if( RegCreateKey( HKEY_LOCAL_MACHINE,
TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunServices"),
&hKey ) != ERROR_SUCCESS )
{
//DebugOut( "RegCreateKey() error!");
return FALSE;
}
}

dwValueType = REG_SZ;
dwStrCb = 128;

// Take value
if( RegQueryValueEx(hKey,
SERVICE_NAME,
0,
&dwValueType,
(LPBYTE)lpszStr,
&dwStrCb ) == ERROR_SUCCESS )

{
// Find this key value
if( strcmp( lpszStr, lpszName )==0 )
//if( _tcscmp( lpszStr, lpszName )==0 )
{
// Remove the service
if( dwType == RSP_UNREGISTER_SERVICE )
{
if( RegDeleteValue( hKey, SERVICE_NAME ) == ERROR_SUCCESS )
{
RegCloseKey ( hKey );
return TRUE;
}
RegCloseKey( hKey );
return FALSE;
}
// Already exist service
if( dwType == RSP_SIMPLE_SERVICE )
{
//DebugOut("Already registed!");
RegCloseKey( hKey );
return TRUE;
}
}
// Not find it
} // No this value

// Unregiste return
if( dwType == RSP_UNREGISTER_SERVICE )
{
RegCloseKey( hKey );
return TRUE;
}

// No this value then create it
if( dwType == RSP_SIMPLE_SERVICE )
{
dwStrCb = 128;

// Set value
if( RegSetValueEx(hKey,
SERVICE_NAME,
0,
REG_SZ,
(CONST BYTE *)lpszName,
dwStrCb ) != ERROR_SUCCESS )
{
//DebugOut("RegSetValueEx() error!");
RegCloseKey( hKey );

return FALSE;
}
RegCloseKey( hKey );
return TRUE;
}

// Unknow type
RegCloseKey( hKey );
return FALSE;
}
jun 2000-06-27
  • 打赏
  • 举报
回复
我写进去了,注册表里的值也是对的,但是对于REG_DWORD的值我读出来却不对,请问为什么?我是这么读的:

//hKey已经获得
lpName = "Port";
lpType = REG_DWORD;
lpLen = sizeof(DWORD);
BYTE PortValue;
if (RegQueryValueEx(hKey,lpName,NULL,&lpType,&PortValue,&lpLen) != ERROR_SUCCESS)
{
return;
}
m_Port = (UINT)PortValue;
注册表中Port的值是333,但是PortValue的值却是:77'M',转换后的m_Port值是77,
请问为何?
bzjbest 2000-06-27
  • 打赏
  • 举报
回复
HKEY hKey;
unsigned long ulIP;
if(RegCreateKey(HKEY_CURRENT_USER, "Software\\Max Studio\\Chat", &hKey)==ERROR_SUCCESS)
{
RegSetValueEx(hKey, "m_strYourName", 0, REG_SZ, (BYTE*)(LPCTSTR)m_strYourName, m_strYourName.GetLength());
RegSetValueEx(hKey, "m_nPort", 0, REG_DWORD, (BYTE*)&m_nPort, sizeof(int));

ulIP = b1<<24 | b2<<16 | b3<<8 | b4;
RegSetValueEx(hKey, "ipDestination", 0, REG_DWORD, (BYTE*)&ulIP, sizeof(DWORD));
}
jun 2000-06-27
  • 打赏
  • 举报
回复
谢谢指教,我还要将Port这个UINT写到注册表中应该怎么写?我写的不行。

UINT PortValue;
UINT m_Port=80;
lpName = "Port";
PortValue= m_Port;
RegSetValueEx(hKey,lpName,0,REG_DWORD,(CONST BYTE *)PortValue,sizeof(UINT));
RegCloseKey(hKey);

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧