VC 編寫 stdcall 方式的DLL

2021-06-21 15:13:49 字數 1272 閱讀 7080

vc工具:vs2005

delphi 工具: delphi 7

vs2005 新建dll

新建-專案 儲存時選擇 dll

**// testdll.cpp : 定義 dll 應用程式的入口點。

//#include "stdafx.h"

#ifdef _managed

#pragma managed(push, off)

#endif

bool apientry dllmain( hmodule hmodule,

dword  ul_reason_for_call,

lpvoid lpreserved

)hresult __stdcall  testadd(int i, int j)

#ifdef _managed

#pragma managed(pop)

#endif

**很簡單,就乙個 testadd

注意 不能使用vc的 extern"c" __declspec( dllexport ) hresult __stdcall  testadd(int i, int j)

下一步,新建乙個def檔案(使用記事本新建,儲存為def),vc預設為mfc的dll帶def的,其他的不帶。

def**:

library      "testdll" 

exports

testadd   @1

然後將def加入vs2005,vc6工程中,只要有def檔案並將其新增到工程中,vc6就可以自動地生成dll與其相應的lib檔案了。

但是vs2005不一樣,需要指定def檔案

工程 > 屬性中的鏈結器,然後找到"輸入"這一項. 在 "模組定義檔案" 中輸入 testdll.def.

然後編譯

delphi 呼叫

testaddfun = function (i: integer; j: integer): hresult; stdcall;

vartestfun: testaddfun;

h: hmodule;

begin

h := loadlibrary('testdll.dll');

if h <> 0 then

testfun := getprocaddress(h, 'testadd');

if assigned(@testfun) then

showmessage(inttostr(testfun(1, 20)));

freelibrary(h);

至此,呼叫成功

vc 中stdcall與cdecl的相關知識

stdcall 和 cdecl 都屬於呼叫方式 一般我們在宣告函式時都沒有顯示的說明呼叫方式,因為不同語言都有不同的預設呼叫方式 能否實現變參 stdcall stdcall是pascal程式的預設呼叫方式,通常用於win32 api中,由主呼叫函式採用從右到左的壓棧方式進行引數壓棧,然後由被呼叫者...

Action的編寫方式

第一種 建立普通類 不繼承任何類,不實現任何介面 public class helloaction 第二種建立類,實現介面 action public class useraction implments action 第三種建立類,繼承類 actionsupport public class pe...

VC 使用 MinGW編寫的dll

參考文章 因為對ia32彙編格式不熟悉 所以寫了一些用c內嵌gnu彙編的 來獲取cpu的資訊,想用vc來做介面,c函式用mingw在windows平台編譯,完事遇到乙個問題,gcc編譯出來的dll檔案vc無法識別,於是昨天晚上和今天下午都在查詢資料,下面是我整理來的,不知道這樣用合不合適啊,不合適你...