最近在弄个ocx调用dll的函数,只是掌握vc知识的零星,所以很多小的地方都能给卡住,还好网上很多这样的介绍,下面做一个记录。
首先用ATL创建个工程,加入下面的代码。

Code
#include <string.h>
extern "C" __declspec(dllexport) void MsgNoParam()
{
AfxMessageBox("com sa ha mi da");
}
//显示参数内容
extern "C" __declspec(dllexport) void MsgParam(CString str)
{
AfxMessageBox(str);
}
注意:这里的"C"必须是大写的,不然就会出错哦
error C2537: 'c' : illegal linkage specification
dllexport表示这个函数是由外部调用的。编译工程生成dll。
生成后,现在就可以在ocx中调用它了。

Code
void RefDllMethod()
{
HINSTANCE hDllInst;
hDllInst=::LoadLibrary("dll文件路径");
if(hDllInst != NULL)
{
//不带参数的调用-----------------------
//FARPROC proc;
//proc=GetProcAddress(hDllInst,"MsgNoParam");
//--------------------------------------
//带参数的调用----------------------------
typedef void (FAR __cdecl *TEST)(CString);
TEST proc;
proc =(TEST)GetProcAddress(hDllInst,"MsgParam");
//--------------------------------------------
if(proc==NULL)
{
AfxMessageBox("找不到该函数。");
}
else
{
proc("Success!");
}
}
else
{
AfxMessageBox("指定的路径打不开或不存在该文件。");
}
FreeLibrary(hDllInst);
}
注意:由于DLL中TEST的定义为C语言调用规范,因此TEST前一定要用__cdecl,而VC中常用的__stdcall是PASCAL调用规范,不可以的。一定要注意。
由于知识尚浅,只了解皮毛,目前还在学习的过程中,希望对想了解这方便的人有一点点的帮助。