原 C 跨平台呼叫C 的函式指標

2022-03-13 00:55:33 字數 1909 閱讀 6488

此文主要講一下列子,通過列子就能很清楚的看到是如何用c#去呼叫c++的**的.

列子:在c++的乙個標準win32 api 庫cclic.dll中有乙個函式void* winapi getfunctionaddress(unsigned int sn);此函式通過傳sn序號得到函式指標即乙個函式的位址.之後再通過返回回來的位址進行其它函式的呼叫

那麼我們必須知道.乙個sn號對應的函式結構如 sn=1 -> bool winapi ccaskserverlicenseinfo(const char* server_address,unsigned short port,pcclic_info plicenseinfo)

在其中typedef struct _cclic_info cclic_info,*pcclic_info;

此列的目的就是通過c#呼叫 ccaskserverlicenseinfo 函式.

[dllimport(

@"cclic.dll")]

public

static

extern

system.intptr matrix(system.uint32 sn);//聲名入口函式

//定義函式指標模型

public

delegate

system.int32 ccaskserverlicenseinfohandle(system.string servername, system.uint16 port, system.intptr ptr);

public

static

licenseinfo getlicentinfo(string server, system.uint16 port)

[structlayout(layoutkind.sequential, charset 

=charset.ansi)]

public

struct

licenseinfo

[dllimport(

@"cclic.dll")]

public

static

extern

system.intptr matrix(system.uint32 sn);

public

delegate

system.int32 ccaskserverlicenseinfohandle(system.string servername, system.uint16 port, system.intptr ptr);

public

static

licenseinfo getlicentinfo(string server, system.uint16 port)

{system.intptr fptr 

=matrix(8);

ccaskserverlicenseinfohandle ccaskserverlicenseinfo 

=marshal.getdelegateforfunctionpointer(fptr, 

typeof

(ccaskserverlicenseinfohandle)) 

asccaskserverlicenseinfohandle;

licenseinfo info 

=new

licenseinfo();

intptr infoptr 

=marshal.alloccotaskmem(marshal.sizeof(info));

ccaskserverlicenseinfo(server, port, infoptr);

info 

=(licenseinfo)marshal.ptrtostructure(infoptr, 

typeof

(licenseinfo));

return

info;

c 使用指標呼叫函式

首先要知道一點,方法名其實就是指向多個引數的指標,裡面存放著位址。使用系統api呼叫動態庫的 hmodule hdll loadlibrary l c users administrator desktop 12.dll 載入動態庫 typedef void winapi csayhi 定義乙個無返...

C 空指標呼叫函式

在c 中是允許空指標呼叫函式的,但是在我們使用空指標呼叫成員函式的時候,一定要注意有沒有用到this指標如果用到了this指標要加以判斷以保證程式的健壯性。例如以下程式 class person void getfd int fd 0 void test01 intmain 我們執行的時候,很顯然報...

C 實現跨平台

要建立乙個c 跨平台的應用,亦難也易。難在什麼?所謂應用,自是實際應用,牽涉面很廣。一般來說,至少有如下幾個方面需要我們考慮 0.語言及基礎庫 1.作業系統相關的 2.網路 3.資料庫 4.介面 其它自然還有很多,如xml,加解密等等,這裡先討論前面列出的幾項。眾所周知,不同的作業系統即時是相同的函...