2014-02-17 24 views
1

我在C#中遇到了Dll問題。 Dll是用C++語言編寫的。我想使用DLL的結構和方法。我不知道如何在C#中編寫代碼,我使用結構和方法。如何在C#語言中使用C++ dlll?

C++代碼: 這是標題。

#ifndef _FILTERS_H_ 
#define _FILTERS_H_ 

#ifdef __DLL 
# define DLLIMPORT __declspec (dllexport) 
#else 
# define DLLIMPORT __declspec (dllimport) 
#endif 

#define _AFXDLL 

#include <string> 
#include <iostream> 
#include <sstream> 
#include <fstream> 
#include <cstdlib> 
#include <math.h> 
#include <Wingdi.h> 
///#include <windows.h> 
///#include <afxwin.h> 
#include <windef.h> 

using namespace std; 
///using namespace Gdiplus; 
///#pragma comment (lib,"Gdiplus.lib") 
#pragma comment (lib,"Gdi32.lib") 
///using namespace System; 
///using namespace System::Drawing; 



#ifdef EXPORTING_DLL 
#ifndef _RDT_ 
extern struct __declspec(dllexport) SCIOX_bitmap{ 
    int width; 
    int height; 
    int color_depth; 
    int* points; 
}; 
#endif 
extern __declspec(dllexport) SCIOX_bitmap median(SCIOX_bitmap image, int size); 
#else 


#ifndef _RDT_ 
class __declspec (dllimport) DllClass 
{ 
    public: 
    DllClass(); 
    virtual ~DllClass(void); 

    private: 

}; 
#endif 


#ifndef _RDT_ 
#define _RDT_ 
#endif 

#endif /* _FILTERS_H_ */ 

我想叫中位數(屬性);以C#語言。

坦克你的回答和對不起壞英語

+1

大量的例子在這裏,請搜索本: DllImport –

+4

如果您對pinvoke不夠了解,甚至無法啓動,那麼這絕對不是您想要開始的那種功能。地板墊下隱藏着非常陰暗的內存管理問題。強烈建議使用C++/CLI。 –

+0

不要在類型上使用'__declspec(dllexport)'。在普通的舊數據上,比如'SCIOX_bitmap',它沒用。在有特色的類中,比如'DllClass',它是有害的。習慣將接受「this」指針的全局函數導出爲明確的參數。他們可以調用該對象的成員函數作爲實現的一部分,但成員函數不能以任何輕微便攜的方式導出。 –

回答

1

謝謝,我用「的PInvoke互操作助理」,但我有異常的DLL沒有找到

無法加載DLL「filter.dll」:ZadanýMODUL SA nepodarilonájsť。 (異常來自HRESULT:0x8007007E)

這是我在C#代碼調用函數從DLL

[System.Runtime.InteropServices.StructLayoutAttribute 
(System.Runtime.InteropServices.LayoutKind.Sequential)] 
public struct SCIOX_bitmap 
{ 
    public int width; 
    public int height; 
    public int color_depth; 
    public IntPtr point; 
}; 

[System.Runtime.InteropServices.DllImportAttribute 
(@"filter.dll", EntryPoint = "median")] 
private static extern SCIOX_bitmap median(SCIOX_bitmap image, int size); 

它在按鈕的代碼:

string url = @"url"; 
pictureBox1.Load(url); 
SCIOX_bitmap a = new SCIOX_bitmap(); 
a.height = pictureBox1.Height; 
a.width = pictureBox1.Width; 
var source = new BitmapImage(new System.Uri(url)); 
int b = source.Format.BitsPerPixel; 
a.color_depth = (4 * b) * b; 
a.point = pictureBox1.Handle; 

a = median(a, 8); 
+0

請注意,所有這些都不是C++ - 沒有辦法與C++互操作,這裏的層級接口是純C的。沒有類,只是函數和結構。 Tihs仍然是正確的答案。沒有C++級別的互操作機制。 +1。 – TomTom