2013-03-05 55 views
0

我正在嘗試將C++中的原生DLL導入到C#中。我有一個小問題。「DLLNotFoundException」?

這裏是我的C#代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 

namespace test 
{ 
    class Program 
    { 
     [DllImport("hello2dll.dll")] //I didn't know what to name it :'(
     private static extern void SayHi(); 

     static void Main(string[] args) 
     { 
      while (true) 
      { 
       Console.ReadKey(); 
       SayHi(); 
      } 
     } 
    } 
} 

下面是從DLL main.h:

#ifndef __MAIN_H__ 
#define __MAIN_H__ 

#include <windows.h> 

/* To use this exported function of dll, include this header 
* in your project. 
*/ 

#ifdef BUILD_DLL 
    #define DLL_EXPORT __declspec(dllexport) 
#else 
    #define DLL_EXPORT __declspec(dllimport) 
#endif 


#ifdef __cplusplus 
extern "C" 
{ 
    #endif 

    void DLL_EXPORT SayHi(); 

    #ifdef __cplusplus 
} 
#endif 

#endif // __MAIN_H__ 

然後在這裏是從DLL的main.cpp:

#include "main.h" 

#include<windows.h> 

void SayHi() 
{ 
    MessageBox(HWND_DESKTOP, "Hello!", "Hello!", 0); 
} 

所以我試圖通過將它放入system32來訪問DLL,然後我試圖通過將它複製並粘貼到Visual c#中來將其添加到項目中,但如此fa我沒有成功。我有點失望,它不起作用,但誰知道。

+1

你會得到什麼錯誤?或者你只是沒有消息框? – 2013-03-05 03:58:05

+0

1. 32位或64位窗口?許多問題是由於缺少適合.dll的平臺而造成的。 – Petesh 2013-03-23 23:58:18

回答

0

我不知道爲什麼它的失敗,而我想創建一個CLR兼容機組裝

您可以使用tblimp那麼你可以添加參考組件,而不是將其導入一個CLR兼容機組裝。

相關問題