2015-10-04 98 views
-1

我在使用DllImport函数时遇到问题!DllImport未找到C++

using namespace System; 
using namespace System::Runtime::InteropServices; 

[DllImport("user32.dll")] 

运行时它不会显示属性。我正在使用C++的Windows窗体。我真的很喜欢WindowsForms。

namespace Projekt3{ 

using namespace System; 
using namespace System::ComponentModel; 
using namespace System::Collections; 
using namespace System::Collections::Generic; 
using namespace System::Windows::Forms; 
using namespace System::Data; 
using namespace System::Drawing; 
using namespace System::Runtime::InteropServices; 
using namespace System::Threading::Tasks; 
using namespace System::IO; 
using namespace std; 



public ref class next : public System::Windows::Forms::Form 
{ 
    [DllImport("user32.dll")] 
    public static extern IntPtr FindWindow(String sClassName, String sAppName); 

    [DllImport("user32.dll")] 
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); 

    [DllImport("user32.dll")] 
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id); 


public: 

    next(void) 
    { 


     InitializeComponent(); 
     // 
     //TODO: Konstruktorcode hier hinzufügen. 
     // 
    } 
public: enum fsmodifiers 
{ 

    NOMOD = 0x0000, 
    ALT = 0x0001, 
    CTRL = 0x0002, 
    SHIFT = 0x0004, 
    WIN = 0x0008, 

}; 
+0

尝试添加'DllImportAttribute'。同样在C++/CLI中,您可以直接调用Win32 API! –

+0

没有为我工作。感谢您的快速回答。 – luckeRRR

回答

1

只要我们可以看到它,您的代码工作得非常好。例如,该程序

using namespace System; 
using namespace System::Runtime::InteropServices; 

[DllImport("user32.dll")] 
extern int MessageBox(System::IntPtr hwnd, System::String^ text, System::String^ caption, 
    unsigned int uType); 

int main(array<System::String ^> ^args) 
{ 
    MessageBox((System::IntPtr)0, "foo", "bar", 0); 
    return 0; 
} 

产生预期的消息框。

这是非常值得指出的是,从C++/CLI使用p/invoke似乎是一个非常没有意义的练习。您可以直接链接到非托管代码。这个C++/CLI程序写成这样会更自然:

#include <Windows.h> 

int main(array<System::String ^> ^args) 
{ 
    MessageBoxA(0, "foo", "bar", MB_OK); 
    return 0; 
}