有关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 说明是高位补零~!!(内存右边是高位,但每个字节中左边是高位~!)
#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 说明是高位补零~!!(内存右边是高位,但每个字节中左边是高位~!)
相关文章推荐
- exe函数与dll函数的相互调用
- Jquery中的一些细节(一)--------------->Jquery加载调用多个函数(方法)需要注意的细节
- 理解ATL中的一些汇编代码(通过Thunk技术来调用类成员函数)
- win7下修改注册表问题 模块"jscript.dll"已加载,但对DllRegisterServer的调用失败,错误代码为0x80004005 我是WIN7
- 单例模式将配置文件加载在jvm内存,修改配置文件,可调用reload方法重新加载至内存
- win7下修改注册表问题 模块"jscript.dll"已加载,但对DllRegisterServer的调用失败,错误代码为0x80004005 我是WIN7
- 有关Flash中与Java调用时候注意的一些事项
- 编写一个函数,此函数使用动态存储分配来产生一个字符串的副本。例如函数为strclone,则调用p=strclone(str),将会为一个新的字符串分配和str占内存大小相同的一个字符串,并将字符串st
- DLL中用malloc分配了一块内存,但是在exe程序中释放引发的错误:其原因可能是堆被损坏,这也说明 **.exe 中或它所加载的任何 DLL 中有 bug。
- 父类中调用自身定义的虚函数,其中若修改派生类定义的变量,无效
- 操作系统安全配置下关于一些注册表条目的修改
- 与socket有关的一些函数介绍
- js调用其他js文件的函数,以及js加载过程
- 练习 3-1 在上面有关折半查找的例子中,while循环语句内共执行了两次测试,其实只要一次就足够(代价是将更多的测试在循环外执行)。重写该函数,使得在循环内部只执行一次测试。
- 体验了一下msdn2012,挺好用的,可以找到所有的函数,进行调用,还有例子。
- 主调函数分配内存的两次调用
- PPC系统内部和注册表的一些解释与修改
- 最完整的:JAVA调用ORACLE的存储过程、函数的返回结果集例子
- C++类对象内存模型与成员函数调用分析(下)
- c#中用windows api函数修改内存数据