VC DLL實作,calling convention 必需是__stdcall
view plain
CODE:
#include <windows.h>
#include <stdio.h>
int __stdcall GetIntFromVC(int x )
{
x *= 2;
return x;
}
int __stdcall GetStrFromVC(char* pbstr, char *lpBuffer)
{
return sprintf(lpBuffer, "*** %s ***", pbstr);
}
避免 name mangling ,加入 .def
view plain
CODE:
LIBRARY test
EXPORTS
GetIntFromVC @1
GetStrFromVC @2
Perl call dll
view plain
CODE:
#! perl -slw
use strict;
use Win32::API;
#傳入一個參數,回傳一個值 ,N: value is a number (long)
my $fun1 = new Win32::API('test.dll', 'GetIntFromVC', [...]
In VC DLL
function 前不需要加入 __declspec(dllexport),要匯出的function名稱加入 .def 即可,避免name mangling
function 之 calling convertion 為 __stdcall
ex. int __stdcall GetCPUSpeed()
使用.def 作出的 Dll,可被 VC 及 VB 程式呼叫
VB 傳參數進 VC DLL
例一 : Call By Reference
In VB,若function宣告為
Private Declare Function GetStrFromVC1 "z:DllDebugDll2.dll" ( ByRef PA as integer, ByRef str1 as String) As String
呼叫
view plain
Visual Basic:
Dim strRet as String
Dim PA as Integer
Dim strOutStr as [...]
21 十二月, 2005
Posted by: asd In: 科技新知| 軟體使用
之前用 VC 時,若 WorkSpace 包含 Dll 專案,會產出.lib及.dll,.lib通常會設定統一集中在lib目錄下,dll必須與執行檔同個目錄才能執行。所以說必須手動再把 dll 拷貝到主程式 Active Project的工作目錄下,若不只一個Dll專案那就累人了,必須手動逐一copy。其實只要在Dll專案 Setting->Post build step 新增 command 代表當此專案build完後要作什麼事情,可以下
XCopy ..liblibText2Phone.dll ..gengrm /Y /F /D
/Y /F /D是xcopy的參數,分別表示
/Y: 不要提示您確認是否要覆蓋一個已經存在的檔案
/F: 在複製時顯示來源及目的檔案的全部檔名
/D: 複製在指定日期當天或之後發生變更的檔案。如果沒有給日期, 只複製那些來源檔案日期比目的檔案日期為新的檔案。
Calling a DLL C++ function from a VC++ application
Calling a DLL C++ class from a VC++ application
Calling a DLL C++ function from a VB application
Calling a DLL C++ class from a VB application
Loading a C++ DLL dynamically from a VC++ application