I am wrote a visual c++ win32 console app, and i wrote it and tested it in开发者_StackOverflow中文版
win32 console project
. then i switch to
win32 project
and imported all the source files and created a dll for it. by mark the class i want to export as
#define DllExport __declspec( dllexport )
class DllExport theClass {
}
it works and the dll is generated. then i created a another c# project and want to add the dll to the project. by reference->add reference-> browser. then i select that dll. then it is gives me an error
a reference to the '''''''.dll could not be added. please make sure that the file is accessible, and that is a valid assembly or com component.
anyone knows where i did wrong to generate/import the dll?
thanks
The Add Reference dialog can only work for DLLs that contain metadata (managed code) or a type library (a COM server). Your DLL doesn't fit that bill, you can only use the [DllImport] attribute in C# code to use the P/Invoke marshaller to call an unmanaged DLL entrypoint.
That can not be a native C++ class, like you are trying to do, there is no reliable mechanism for managed code to allocate unmanaged memory and call the constructor (and destructor) of a native C++ class. Short from the difficulty of finding the constructor and destructor code, there is no way for the P/Invoke marshaller to know the size of the object. The C++ language doesn't generate the metadata necessary to know this required information.
If you want to pursue P/Invoke then write an plain global function, decorated with extern "C", __declspec(dllexport) and (optionally) __stdcall.
If you want to export a C++ class then the only avenue is using the C++/CLI language and write a "ref class" wrapper for the native C++ class.
Or you could write a COM coclass, the universal glue in Windows. Very well supported by .NET, probably not something you want to pursue if you never wrote COM code before. ATL is the best way to get one going.
精彩评论