I know that this is supposed to be simple, but I just can't get it to
work--I've been at it for several days!!!
Here's my simple c++ class:
// sayHello.h
#include <string>
using namespace std;
class sayHello
{
string sComment;
public:
sayHello(string sCmnt);
~sayHello();
void SayIt();
};
---I compile this class as a win32 dll
===========================================
I then wrap it in a managed cpp class which is compiled
as a .net dll using the /clr switch
// sayHelloWrap.cpp
#using <mscorlib.dll>
#include "sayHello.h"
using namespace System;
public __gc class sayHelloWrap
{
private:
sayHello* pHello;
public:
sayHelloWrap(string str )
{ pHello = new sayHello(str); }
~sayHelloWrap(){ delete pHello; }
void SayItWrap(){ pHello->SayIt(); }
};
========================
so far, so good
I reference the sayHelloWrap.dll in the assembly for
the main application project.
The lines within main() look like this:
sayHelloWrap h = new sayHelloWrap("Say this!");
h.SayIt();
======
I get linker errors:
" Type sayHelloWrap has no constructors defined "
" 'sayHelloWrap' does not contain a definition for 'SayItWrap' "
What on earth is happening?
It is supposed to "Just Work"
Thanks for answering this question, again... I'm just not getting it.
Then in my main function in the c# file I
Dino Chiesa [Microsoft] - 20 Dec 2004 16:25 GMT
there are examples of this in the .NET SDK.
<netsdk>\Samples\Technologies\Interop\PlatformInvoke
Have you defined your C++ header properly ? I get those sort of errors
when the .h file does not document all the public members of the class.
-Dino
>I know that this is supposed to be simple, but I just can't get it to
> work--I've been at it for several days!!!
[quoted text clipped - 57 lines]
> Thanks for answering this question, again... I'm just not getting it.
> Then in my main function in the c# file I
Jeff Sloan - 20 Dec 2004 20:17 GMT
Dino,
As you can see my header file is posted, and it all member functions are
public.
My problems with all of the documentation I have seen so far for
platform invoke is that they ALL import discrete functions (never
classes, or members of classes) and they ALL just import functions from
the microsoft standard dlls. I can't fine ONE example that uses a custom
class. Actually I take that back, there is one in the C++ migration
guide, but it does not include all of the source code or the specific
compiler and linker flags to make it work (my source code above is a
simplified version of the example in the MS docs). In other words, it
DOES NOT work as the migration guide specifies it. I have tried all the
different combinations I could think of. Creating the C++ dll as
dynamic, static, with and without /clr. I've done likewise with the
wrapper class. I've forgotten all the ways I tried it--suffice it to say
that I've spent about a full week trying to make this work.
If there is anybody has a simple project that shows how to import a C++
class into C# I'd really appreciate it if you could zip it up and send
it to me:
jeffreymsloan at msn dot com
I'm beginning to think it's not really possible to import functions, and
certainly not classes from a CUSTOM (user written) C++ dll.
-Jeff
Alessandro Angeli [MVP::DigitalMedia] - 20 Dec 2004 20:53 GMT
> Here's my simple c++ class:
>
[quoted text clipped - 14 lines]
>
> ---I compile this class as a win32 dll
How can you compile this into a DLL? Where are your actual
method implementations? Is this exactly what you compile,
without exports or .defs or anything else? What are your
compilers settings?
There may be other things wrong with the rest of your code,
but we can start from here.

Signature
// Alessandro Angeli
// MVP :: Digital Media
// a dot angeli at psynet dot net
Jeff - 20 Dec 2004 22:09 GMT
Hi Alessandro,
Thanks for your comments. After I posted this, I realized I forgot to
export my class and or functions. I did that and a couple of more
tweaks to the compiler andlinker options and FINALLY got it to work.
Thanks.
-js