2011-02-10 40 views
1

幫助!C++/CLI圍繞C靜態庫管理的包裝器

我完全疲憊/沮喪似乎是一個相當容易的任務。我不確定我做錯了什麼;更別說如果我做得正確。我需要在開發WPF應用程序(VS 2010,C#4.0)時使用現有庫(一個C靜態庫 - 超過100,000行直C代碼)。哦,我無法觸摸現有的C代碼 - 按原樣使用!

我已經閱讀了很多帖子(高級主題,how-to等),但我對C++/CLI很陌生,它只是沒有意義。從我讀過的最好的辦法是包的C靜態庫如下:

非託管c靜態庫< ---> C++/CLI託管包裝DLL < ---> 管理WPF應用程序

這在剝離下來的C頭文件:

/* Call this function to execute a command. */ 
int issue_command(int command, long param1, long param2); 

/* Completion call back function; you must supply a definition. */ 
extern int command_completed(int command, long param1, long param2); 

struct struct_command_str 
{ 
     char command_str[10]; 
     char param1_st[2]; 
     char param2_st[2]; 
     char success; 
}; 

/* You must supply definitions to the following extern items. */ 
extern int command_status; 
extern struct struct_command_str command_str; 

的問題(S):

我似乎無法正確執行的是爲回調函數和兩個外部項目(command_status和struct command_str)提供C++/CLI實現。

有人可以提供一個示例C++/CLI實現上述缺少回調函數和外部?

在此先感謝您的幫助。

在你的C++/CLI託管包裝項目
+0

您可以在C++/CLI實現中至少包含一次失敗的嘗試嗎? – 2011-02-10 16:50:10

回答

3

,加2個文件:

.c文件:

extern void doSomething(); 

int command_status = 0; 

struct_command_str command_str = { "command1", "p1", "p2", 't' }; 

int command_completed(int command, long param1, long param2) { 
    ... 
    command_status = 1; 
    ... 
    doSomething(); 
    ... 
    command_status = 2; 
    ... 
    return 3; 
} 

一個CPP文件

void doSomethingManagedWrapper() { 
    ... 
    call managed code 
    ... 
} 

void doSomething() { 
    doSomethingManagedWrapper(); 
} 
+0

謝謝,這很有幫助。 – LabDog 2011-02-11 18:30:58

1
當你在你的c實現這些

++/cli模塊,使用c頭文件中顯示的相同簽名,但前綴爲extern "C"

還在C頭文件的#include周圍放置了一個extern "C"塊。

+0

Rolo上面的評論似乎解決了這些問題。我從來沒有嘗試過你的建議,但欣賞這些信息(爲了將來的使用而收拾起來)。 – LabDog 2011-02-11 18:33:10