楼上的是C# call C++ dllhttp://www.daniweb.com/forums/thread90053.html
调用C#的DLL 应该和调用普通的COM接口完全一样 主要是Assmebly要写成COM可见的 // CPPClient.cpp: Defines the entry point for the console application. // C++ client that calls a managed DLL.#include "stdafx.h" #include "tchar.h" // Import the type library.#import "..\ManagedDLL\bin\Debug\ManagedDLL.tlb" raw_interfaces_only using namespace ManagedDLL; int _tmain(int argc, _TCHAR* argv[]) { // Initialize COM. HRESULT hr = CoInitialize(NULL); // Create the interface pointer. ICalculatorPtr pICalc(__uuidof(ManagedClass)); long lResult = 0; // Call the Add method. pICalc->Add(5, 10, &lResult); wprintf(L"The result is %d\n", lResult); // Uninitialize COM. CoUninitialize(); return 0; }
看了这个例子,你应该会了,这样声明一下就可以用了
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr GetWindowDC(IntPtr hwnd);以前别人回复的,我转
// CPPClient.cpp: Defines the entry point for the console application.
// C++ client that calls a managed DLL.#include "stdafx.h"
#include "tchar.h"
// Import the type library.#import "..\ManagedDLL\bin\Debug\ManagedDLL.tlb" raw_interfaces_only
using namespace ManagedDLL;
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL); // Create the interface pointer.
ICalculatorPtr pICalc(__uuidof(ManagedClass)); long lResult = 0; // Call the Add method.
pICalc->Add(5, 10, &lResult); wprintf(L"The result is %d\n", lResult);
// Uninitialize COM.
CoUninitialize();
return 0;
}
#include <windows.h>
#include <tchar.h>
#using <System.dll>
#using <System.Windows.Forms.dll>int __declspec(dllexport) f1(LPTSTR msg)
{
System::Windows::Forms::MessageBox::Show("hello from dll");
::MessageBox(::GetDesktopWindow(), msg, TEXT("info"), MB_OK);
return 0;
}
测试的客户端, 控制台程序:// cl /clr testclrdll.cpp clrdll.lib
#include <windows.h>
#include <tchar.h>int f1(LPTSTR msg);int main()
{
f1("消息");
return 0;
}