您的位置:首页 > 其它

有关EXE加载和调用DLL函数的例子;用ATL函数修改注册表和内存存贮的一些认识(一)

2005-08-08 15:08 465 查看
/////////////////////////////////////////////RegEditExe.cpp   (EXE工程的)
#include "RegExcute.h"
int _tmain(int argc, _TCHAR* argv[])
{
 int x;
 long i;
 printf( "Input your action:/n" );
 printf( "1:Add/n" );
 printf( "2:Remove/n" );
 scanf( "%d", &x );
 act = Action ( x );
 CRegExcute excute;
 i = excute.RegOperation( act );
 printf( "result:%l/n",i );
 return 0;
}
///////////////////////////////////////////////RegExcute.h   (DLL工程的)
#include "atlbase.h"

#define IN_SUCCESS      1
#define IN_FAILED            2
#define OUT_SUCCESS  3
#define OUT_FAILED        4
#define OPEN_FAILED     5
#define REG_ERROR      100
enum Action
{
 In = 1,
 Out = 2,
}act;
typedef struct CValue_1
{
 DWORD dwValue1;
}Add_Value;
class CRegExcute :
 public CRegKey
{
public:
 __declspec(dllexport) CRegExcute(void);
 __declspec(dllexport) ~CRegExcute(void);
 __declspec(dllexport) long RegOperation( Action act );
public:
 LPCTSTR pszValueName;
};
//////////////////////////////////////////////////////RegExcute.cpp  (DLL工程的)
#include "./regexcute.h"
CRegExcute::CRegExcute(void)
{
}

CRegExcute::~CRegExcute(void)
{
}

long CRegExcute::RegOperation( Action act)
{
 CRegKey rk;
 LPCTSTR lp="SYSTEM//ControlSet001//Services//RemoteAccess//Interfaces//4//Ip";
 if( rk.Open( HKEY_LOCAL_MACHINE, lp ) == ERROR_SUCCESS )
 {
  LPCTSTR pszValueName = "trial";
 
  switch ( act )
  {
  case In:
   {
    Add_Value* pValue = new Add_Value;
    
    pValue->dwValue1 = 0x1234;
    
    const ULONG nBytes = sizeof( Add_Value );
    
    if( rk.SetBinaryValue( pszValueName, pValue, nBytes ) == ERROR_SUCCESS )
    {
     delete pValue;
     
     return IN_SUCCESS;
    }
    else
    {
     delete pValue;
     
     return IN_FAILED;
    }
   }
   break;
  
  case Out:
   {
    if( rk.DeleteValue( pszValueName ) == ERROR_SUCCESS )
    {
     return OUT_SUCCESS;
    }
    else
    {
     return OUT_FAILED;
    }
   }
   break;
  
  default:
   {
    return REG_ERROR;
   }
   break;
  }
 }
 else
 {
  return OPEN_FAILED;
 }
}
Build DLL后,
(1)把.dll文件copy到生成exe所在文件夹中
(2)把.lib文件copy到exe工程所在的文件件中
(3)在Project->properities->Linker->Input->Additional Dependencies:中加入.lib文件
*********注:发现:
(1)   这个例子还应用了创建Value和删除Value的例子
(2)   DWORD dwValue1 = 0x1234; 在内存中存贮是:34 12 00 00     说明是高位补零~!!(内存右边是高位,但每个字节中左边是高位~!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐