/*-------------------------------------------------------------------------------------------------------*/ <br />/* LabWindows/CVI调用DLL实用例程(菜农HotPower) */<br />/* 本例程是在CVI自带的simple添加调用mydll.dll的函数MyDLLCdeclFunction() */<br />/* 网上和CVI的例程很少,一般为调用LIB实例,但很多dll都是以*.dll提供的,故本例程很实用。 */<br />/* 本例程主要是调用了三个Windows API函数LoadLibrary(),GetProcAddress(),FreeLibrary() */<br />/* 菜农HotPower@126.com 2008.5.27 于西安大雁塔菜地 */<br />/*-------------------------------------------------------------------------------------------------------*/ <br /><br />/* This is a simple project that will call<br /> * functions from an external DLL */<br />//#define __cplusplus <br /><br />#include <stdio.h> <br />#include <windows.h>//需要的API函数实际在winbase.h中定义<br /><br />#include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */<br />#include <formatio.h><br />#include <userint.h><br />#include "mydll.h"<br /><br />/*-------------------------------------------------------------------------------------------------------*/ <br />typedef long int DLLEXPORT (*DLLCdeclFunction)(char *);//定义函数指针 <br />/*-------------------------------------------------------------------------------------------------------*/ <br /><br />int status;<br />char message[80];<br /><br />int main (int argc, char *argv[])<br />{<br />/*-------------------------------------------------------------------------------------------------------*/ <br /> HMODULE hinstLib; <br /> DLLCdeclFunction DLLFunction; <br /> BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; <br />/*-------------------------------------------------------------------------------------------------------*/ <br /><br /> if (InitCVIRTE (0, argv, 0) == 0) /* Needed if linking in external compiler; harmless otherwise */<br /> return -1; /* out of memory */<br /> /* Tell the dll to run it's user interface */<br /> RunDllUI();<br /> <br />/*-------------------------------------------------------------------------------------------------------*/ <br /> // Get a handle to the DLL module. <br /> hinstLib = LoadLibrary("mydll.dll");//装载动态链接库mydll.dll <br /> // If the handle is valid, try to get the function address. <br /> if (hinstLib != NULL)//成功装载动态链接库mydll.dll <br /> { <br /> DLLFunction = (DLLCdeclFunction)GetProcAddress(hinstLib, (LPCSTR)"MyDLLCdeclFunction");//取函数指针地址 <br /><br /> // If the function address is valid, call the function. <br /> if (fRunTimeLinkSuccess = (DLLFunction != NULL))//dll中有函数MyDLLCdeclFunction() <br /> {<br /> Fmt(message, "message via DLL function\n"); <br /> status = (long int)DLLFunction (message);//调用dll函数!!! <br /> }<br /> // Free the DLL module <br /><br /> fFreeResult = FreeLibrary(hinstLib);//卸载动态链接库mydll.dll <br /> } <br /> // If unable to call the DLL function, use an alternative <br /> if (! fRunTimeLinkSuccess) <br /> {<br /> Fmt(message, "message via alternative method\n"); <br /> MessagePopup ("CVI MessagePopup ", message); <br /> }<br />/*-------------------------------------------------------------------------------------------------------*/ <br /><br /> <br /><br /> /* Call the Stdcall Style DLL Function */<br /> status = MyDLLStdcallFunction("Text buffer 1 being passed from CVI to DLL");<br /> <br /> /* Display returned value from DLL function */<br /> Fmt(message, "MyDLLStdcallFunction status returned: %i", status);<br /> MessagePopup ("CVI MessagePopup ", message);<br /> <br /><br /> /* Call the Cdecl Style DLL Function */<br /> status = MyDLLCdeclFunction("Text buffer 2 being passed from CVI to DLL");<br /> <br /> /* Display returned value from DLL function */<br /> Fmt(message, "MyDLLCdeclFunction status returned: %i", status);<br /> MessagePopup ("CVI MessagePopup ", message);<br /><br /> return 0;<br />}<br /><br /><br /> 相关链接:<a href='http://space.**/Upload/Blog/2008/5/27/3dd1bb60-01d3-48d6-9967-ecece1a2a758.rar'>http://space.**/Upload/Blog/2008/5/27/3dd1bb60-01d3-48d6-9967-ecece1a2a758.rar</a> |
|