打印

LabWindows/CVI调用DLL实用例程

[复制链接]
4873|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hotpower|  楼主 | 2008-5-27 23:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*-------------------------------------------------------------------------------------------------------*/    
/*                LabWindows/CVI调用DLL实用例程(菜农HotPower)                                              */
/*  本例程是在CVI自带的simple添加调用mydll.dll的函数MyDLLCdeclFunction()                                 */
/*  网上和CVI的例程很少,一般为调用LIB实例,但很多dll都是以*.dll提供的,故本例程很实用。                    */
/*  本例程主要是调用了三个Windows API函数LoadLibrary(),GetProcAddress(),FreeLibrary()                    */
/*  菜农HotPower@126.com  2008.5.27  于西安大雁塔菜地                                                    */
/*-------------------------------------------------------------------------------------------------------*/    

/* This is a simple project that will call
 * functions from an external DLL */
//#define  __cplusplus 

#include <stdio.h> 
#include <windows.h>//需要的API函数实际在winbase.h中定义

#include <cvirte.h>    /* Needed if linking in external compiler; harmless otherwise */
#include <formatio.h>
#include <userint.h>
#include "mydll.h"

/*-------------------------------------------------------------------------------------------------------*/    
typedef long int DLLEXPORT (*DLLCdeclFunction)(char *);//定义函数指针 
/*-------------------------------------------------------------------------------------------------------*/    

int status;
char message[80];

int main (int argc, char *argv[])
{
/*-------------------------------------------------------------------------------------------------------*/    
    HMODULE hinstLib; 
    DLLCdeclFunction DLLFunction; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
/*-------------------------------------------------------------------------------------------------------*/    

    if (InitCVIRTE (0, argv, 0) == 0)    /* Needed if linking in external compiler; harmless otherwise */
        return -1;    /* out of memory */
    /* Tell the dll to run it's user interface */
    RunDllUI();
    
/*-------------------------------------------------------------------------------------------------------*/    
   // Get a handle to the DLL module. 
    hinstLib = LoadLibrary("mydll.dll");//装载动态链接库mydll.dll 
   // If the handle is valid, try to get the function address. 
    if (hinstLib != NULL)//成功装载动态链接库mydll.dll  
    { 
       DLLFunction = (DLLCdeclFunction)GetProcAddress(hinstLib, (LPCSTR)"MyDLLCdeclFunction");//取函数指针地址 

             // If the function address is valid, call the function. 
       if (fRunTimeLinkSuccess = (DLLFunction != NULL))//dll中有函数MyDLLCdeclFunction() 
       {
            Fmt(message, "message via DLL function\n"); 
            status = (long int)DLLFunction (message);//调用dll函数!!! 
       }
             // Free the DLL module 

       fFreeResult = FreeLibrary(hinstLib);//卸载动态链接库mydll.dll 
    } 
    // If unable to call the DLL function, use an alternative 
    if (! fRunTimeLinkSuccess) 
    {
       Fmt(message, "message via alternative method\n"); 
       MessagePopup ("CVI MessagePopup ", message);     
    }
/*-------------------------------------------------------------------------------------------------------*/    

    

    /* Call the Stdcall Style DLL Function */
    status = MyDLLStdcallFunction("Text buffer 1 being passed from CVI to DLL");
    
    /* Display returned value from DLL function */
    Fmt(message, "MyDLLStdcallFunction status returned: %i", status);
    MessagePopup ("CVI MessagePopup ", message);
    

    /* Call the Cdecl Style DLL Function */
    status = MyDLLCdeclFunction("Text buffer 2 being passed from CVI to DLL");
    
    /* Display returned value from DLL function */
    Fmt(message, "MyDLLCdeclFunction status returned: %i", status);
    MessagePopup ("CVI MessagePopup ", message);

    return 0;
}


相关链接:http://space.**/Upload/Blog/2008/5/27/3dd1bb60-01d3-48d6-9967-ecece1a2a758.rar

相关帖子

沙发
McuPlayer| | 2008-5-27 23:52 | 只看该作者

DLL有显式调用和隐式调用

LoadLibray+GetAddress方式属于显式,其好处是不需要lib和h文件,并可实现动态加载

链接Lib文件是隐式调用,好处是像调用本地函数一样使用DLL内的函数,缺点是在exe启动的时候逐一加载。

使用特权

评论回复
板凳
hotpower|  楼主 | 2008-5-28 00:01 | 只看该作者

lib的CVI例程太多,dll例程很少...俺也想在此求助问题

CVI能否调用VC的C++的dll???

类的成员如何调用???

在此求助如何用CVI调用RTDXINT.DLL???

使用特权

评论回复
地板
high| | 2008-5-28 14:15 | 只看该作者

McuPlayer,原来如此啊。

我一直纳闷着。
那么这个lib和静态的lib也应该是不一样的。它可能只是个interface.

使用特权

评论回复
5
hotpower|  楼主 | 2008-5-28 20:54 | 只看该作者

lib没难度,dll就不同了~~~

使用特权

评论回复
6
xzhenggen| | 2008-5-30 13:13 | 只看该作者

呵呵,用CVI调用DLL,真是浪费时间。

使用特权

评论回复
7
McuPlayer| | 2008-5-30 18:49 | 只看该作者

关于DLL的interface问题

所有Windows API几乎全是DLL实现,用的是stdcall调用,他约定了参数的操作顺序以及谁负责平衡堆栈。

C或者C++默认的是cdecl调用,所以stdcall的前缀是必须的(可以用宏替换)。

CVI没有用过,但两个程序如果要用C++的Class做Interface,双方一定要用同一个class的定义文件,然后被调用方生成此Class的实例,并传递给调用方--比如CVI模。

于是问题来了,DLL升级,比如增加一个成员函数,h文件变化了,调用者CVI也必须升级。怎么办?虚函数,抽象类。大家共用的一个class是抽象的,纯粹为了接口,真的实现都是这个class的继承类。

然后问题又来了,每个DLL都要有这个h文件给调用者,文本文件很容易误修改也容易导致和被调用者的h有差异,比如Office有贼多的DLL,可以想想得有多少个h文件做Interface啊,M$也觉得maintain这么多的h文件挺累人,于是OLE出来了,COM出来了。

使用特权

评论回复
8
hotpower|  楼主 | 2008-6-1 20:24 | 只看该作者

哈哈~~~COM的真谛快要搞到了...

使用特权

评论回复
9
hotpower|  楼主 | 2008-6-6 02:37 | 只看该作者

哈哈~~~COM菜鸟是毕业了,但dll里的程序只能写不能读很郁闷~~~

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1538

主题

21697

帖子

506

粉丝