YA DllImport question...

Hi -

  Here's another dllimport question. I'm attempting to access native code from managed code. In this case, I'm using the open source tinyXml - a C++ library and wrapping it with a C++/CLI class. I'm able to compile the source code into a win32 dll. I'm exposing a class - TiXmlDocument using __declspec(dllexport). I also expose the base classes. No problem.

  On the C++/CLI side (VS2005), I can create an object pointer, i.e. TiXmlDocument *t ; Here's the header info:

  __declspec(dllimport) class TiXmlDocument
{
public:
  TiXmlDocument(const char *);
}

  You may see where the trouble is be brewing. When I attempt to create an object that take's a file name as an argument, in this case, a xml doc to load, I get the LNK2028 error ... "__thiscall TiXmlDocument::TiXmlDocument(char const *)" .. ref-d in function  "int __clrcall main(cli::array<class System::String ^>^"

The declaration is as follows:

String ^s = "demo.xml";
char* ps = (char*)Marshal::StringToHGlobalAnsi(s).ToPointer();
TiXmlDocument doc(ps);

Here's my win32 dll side:

class __declspec(dllexport) TiXmldocument : public TiXmlNode
{
public:
  TiXmlDocument( const char * docName);
...
}

I wasn't clear if I had to tag the public method definitions also. I tried with and without with the same results.

 

What am I missing

Thanks,
Mike




Answer this question

YA DllImport question...

  • Matt Warren

    I took a step back (again) with some simple hello world stuff. Using VS2005, I created a win32 dll with Exports enabled. I then created a hello world C++/CLI console app (/clr). I included the header file per your suggestion - "HelloWorldWin32Dll.h", referenced the HelloWorldWin32Dll.lib in the linker and everything builds fine. I added another constructor as follows:

    CHelloWorldWin32Dll(const char * doc);

    Now I'm able to create a few basic objects:

    CHelloWorldWin32Dll *c1 = new CHellowRorldWin32Dll();
    CHelloWorldWin32Dll *c2 = new CHelloWorldWin32Dll("doc.xml");

    Also:

    String^ s = L"doc.xml";
    char* ps = (char*)Marshal::StringToHGlobalAnsi(s).ToPointer();
    CHelloWorldWin32Dll *c3 = new CHelloWorldWin32(ps);

    works, too, as does intellisense. Looks like I've got some old school C++ code in the tinyXml libs that !IJW (doesn't just work). Unfortunately, this may point me back to P/Invoke.



  • Pierce Blaylock

    Thank you for the reply. My test C++/CLI app is compiled with just /clr. I've also tried a whole bunch of String/StringBuilder/pin_ptr/char* combinations, etc. but no joy. I noticed that the mangled names in the error list do not match those of dumpbin. I'm assuming this is a VS thing. I've doubled checked paths, etc.

  • John-at-WSRB

    Darn. Hmm. For some reason, the compiler thinks that the class TiXmlDocument on the C++/CLI side is a .NET compatible class. Odd. Perhaps the __declspec(dllimport) is throwing it off. Admittedly, I've never used it, I always just include the header file for the DLL and link with the DLL's lib. Another thing to try: put "#pragma unmanaged" before the declaration and "#pragma managed" when you start to speak C++/CLI again. I'd give it only 20% odds though...



  • Goofs

    Try compiling with /clr instead of /clr:pure



  • YA DllImport question...