DLL调用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/keshuiyun/article/details/7198793

一 VC调用DLL

静态调用:

  1. 把你的youApp.DLL拷到你目标工程(需调用youApp.DLL的工程)的Debug目录下;
  2. 把你的youApp.lib拷到你目标工程(需调用youApp.DLL的工程)目录下;
  3. 把你的youApp.h(包含输出函数的定义)拷到你目标工程(需调用youApp.DLL的工程)目
  4. 录下;
  5. 打开你的目标工程选中工程,选择Visual C++的Project主菜单的Settings菜单;
  6. 执行第4步后,VC将会弹出一个对话框,在对话框的多页显示控件中选择Link页。然
  7. 后在Object/library modules输入框中输入:youApp.lib
  8. 选择你的目标工程Head Files加入:youApp.h文件;
  9. 最后在你目标工程(*.cpp,需要调用DLL中的函数)中包含你的:#include "youApp.h"
注:youApp是你DLL的工程名。

动态调用:

动态调用时只需做静态调用步骤1,然后:

	HINSTANCE hDllInst = LoadLibrary("youApp.DLL");
	if(hDllInst)
	{
		typedef DWORD (WINAPI *MYFUNC)(DWORD,DWORD);
		MYFUNC youFuntionNameAlias = NULL; // youFuntionNameAlias 函数别名
		youFuntionNameAlias = (MYFUNC)GetProcAddress(hDllInst,"youFuntionName");
	
		// youFuntionName 在DLL中声明的函数名
		if(youFuntionNameAlias)
		{
			youFuntionNameAlias(param1,param2);
		}

		FreeLibrary(hDllInst);
	}

二 C#调用DLL

首先,要添加命名空间:
using System.Runtime.InteropServices;
这个命名空间是为了使用:[DllImport]
第二,要静态引用外部库:
在类内部,方法外部,通过"static extern"声明,实例代码如下:
[DllImport("ticketPrinterDll.dll"), CharSet=CharSet.Unicode]
public static extern void ShowMessage();
最后调用这个函数。

展开阅读全文

DLL调用DLL的问题

07-02

最近写程序遇到了一个问题,我再写一个dll文件,这个dll文件用到了opengl的opengl32.dll和glaux32.dll,但是写的程序出现了很多错误,错误出现在gl.h和glaux.h中。。。 rn错误类型是一些未定义的符号。。。 rn我是用VC++2005下用win32写的dll,请大家帮帮我。。。。 rnrn还有,我在dll文件中调用我自己写的dll文件时没有任何错误。。。rnrnrn下面是一些错误信息,其余的错误和这些类似:rn1>e:\program study\projects\dlls\mglmultitex\mglmultitex\gl.h(1135) : error C2144: syntax error : 'void' should be preceded by ';'rn1>e:\program study\projects\dlls\mglmultitex\mglmultitex\gl.h(1135) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-intrn1>e:\program study\projects\dlls\mglmultitex\mglmultitex\gl.h(1135) : error C2146: syntax error : missing ';' before identifier 'glAccum'rn1>e:\program study\projects\dlls\mglmultitex\mglmultitex\gl.h(1135) : error C2182: 'APIENTRY' : illegal use of type 'void'rn1>e:\program study\projects\dlls\mglmultitex\mglmultitex\gl.h(1135) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-intrnrngl.h中第1135行代码:rnWINGDIAPI void APIENTRY glAccum (GLenum op, GLfloat value);rnrnrn如果我在程序中添加#include〈windows.h〉rn就会出现如下错误(只有这一个错误):rn#error Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]rnrn双击本错误,来到如下位置(#error那一行):rn#ifdef _DLLrn#ifndef _AFXDLLrn#error Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]rn#endifrn#endifrnrn然后,我再程序中添加宏定义,#define _AFXDLL,就会出现如下错误:rn#error WINDOWS.H already included. MFC apps must not #include rnrnrn请高手帮我看看这是怎么回事。。。rn谢~~~ 论坛

dll调用dll的问题?

12-26

有一个c/c++ dll,通讯方面的. 应不会c/c++ 所以只能用vb来封装要发送的数据。刚开始写了一个test的事例rnC/c++ dll都调用正常例如:rnPrivate Sub Command5_Click()rn If conntion = False Thenrn pvParam = 1rn '1.设置回调函数rn[color=#800000] Call Dev_SetCallBack(AddressOf OnConnect, AddressOf OnDisconnect, 0, AddressOf OnRecv,rn AddressOf OnError)[/color]rn '2.创建句柄rn Select Case strConfig.Modern Case MODE_UDPrn 'TsHDEV = Dev_Create(MODE_UDP, strConfig.PeerIP, c, strConfig.PeerPort,rn strConfig.LocalPort, 0)rn rn Case MODE_TCP_SERVER '创建客户端连接rn [color=#FF0000]TsHDEV = Dev_Create(MODE_TCP_CLIENT, 0, GetIPdress(strConfig.LocalIP), 0,rn strConfig.LocalPort, 0)[/color] rn Case MODE_TCP_CLIENT '创建服务器侦听rn 'TsHDEV = Dev_Create(MODE_TCP_SERVER, Dev_GetAnyIP(), Dev_GetAnyIP(),rn strConfig.PeerPort, 0, 2)rn End Selectrn ' '3.设定激活回调函数的参rn rn '4.打开通讯端口rn Call Dev_Open(TsHDEV)rn Elsern Call Dev_Close(TsHDEV)rn Form1.Caption = "连接已断开"rn Command5.Caption = "连接"rn conntion = Falsern Text1.Text = ""rn End Ifrn可是用封装成标准低利率执行到该函数地方就不能运行了如:rnPrivate Declare Function Dev_SetCallBack Lib "DevDll.dll" ( _rn ByVal fnConnect As Long, _rn ByVal fnDisconnect As Long, _rn ByVal fnAccept As Long, _rn ByVal fnRecv As Long, _rn ByVal fnError As Long _rn )rnPrivate Function OpenCon(ByVal num As Byte) As Longrnrn Dim peerIP As IPddressrn peerIP.b0 = 192rn peerIP.b1 = 168rn peerIP.b2 = 0rn peerIP.b3 = num 'ip设置rn '---------------------------------------------------------------------------------rn '设置回调函数rn [color=#0000FF]MsgBox "ok"/////////[/color]该行运行rn Call Dev_SetCallBack(AddressOf OnConnect, AddressOf OnDisconnect, AddressOf OnAccept, AddressOf OnRecv, AddressOf OnError)rn [color=#FF0000]MsgBox "ok"[/color] ////////该行不运行这是什么原因呀? rn '创建连接句柄rn TsHDEV = Dev_Create(MODE_TCP_CLIENT, 0, GetIPdress(peerIP), 0, PortNum, 0)rn MsgBox TsHDEVrn If TsHDEV <> 0 Thenrn '打开连接rn Call Dev_Open(TsHDEV)rn Elsern OpenCon = -1rn Exit Functionrn End Ifrn OpenCon = 0rnEnd Function 论坛

关于DLL调用DLL

03-21

如果有两个扩展DLL:A.DLL, and B.DLL。类定义形式如下:rn一个工程中:rn// A.Hrnclass AFX_EXT_CLASS CExampleA : public CObjectrn rnDECLARE_DYNCREATE(CExampleA )rn... class definition ... rn;rnrn另一个工程中:rn// B.Hrnclass AFX_EXT_CLASS CExampleB : public CExampleArn rnDECLARE_DYNCREATE(CExampleB )rn... class definition .. rn;rnrnrn结果出现下面的问题:rnGenerating Code...rnLinking...rnExampleB.obj : error LNK2001: unresolved external symbol "public: static struct CRuntimeClass const CExampleA::classCExampleA" (?classCExampleA@CExampleA@@2UCRuntimeClass@@B)rnDebug/B.dll : fatal error LNK1120: 1 unresolved externalsrnError executing link.exe.rnCreating browse info file...rnrnB.dll - 2 error(s), 0 warning(s)rnrn后来,我又尝试下面的方法rn// A.Hrn#ifdef A_IMPLrn #define CLASS_DECL_A __declspec(dllexport)rn#elsern #define CLASS_DECL_A __declspec(dllimport)rn#endifrnrnclass CLASS_DECL_A CExampleA : public CObjectrn rnDECLARE_DYNCREATE(CExampleA )rn... class definition ... rn;rnrnrnrn// B.Hrn#ifdef B_IMPLrn #define CLASS_DECL_B __declspec(dllexport)rn#elsern #define CLASS_DECL_B __declspec(dllimport)rn#endifrnrnclass CLASS_DECL_B CExampleB : public CExampleArnrnDECLARE_DYNCREATE(CExampleB )rn... class definition ..rn;rnrnrn出现了rnerror C2491: CExampleA::classCExampleA' : definition of dllimport static data member not allowedrnerror C2491: 'CExampleB::classCExampleB' : definition of dllimport static data member not allowed 论坛

没有更多推荐了,返回首页