Asim Patnaik

Hi,

I have created a Win32 dll in C++. Following is the code in the header file:

Code Snippet

#ifdef WIN32CLASSLIBRARY_EXPORTS

#define WIN32CLASSLIBRARY_API __declspec(dllexport)

#else

#define WIN32CLASSLIBRARY_API __declspec(dllimport)

#endif

// This class is exported from the Win32ClassLibrary.dll

extern "C"

{

class WIN32CLASSLIBRARY_API CWin32ClassLibrary {

public:

CWin32ClassLibrary(void);

// TODO: add your methods here.

char* GetString();

};

extern WIN32CLASSLIBRARY_API int nWin32ClassLibrary;

extern WIN32CLASSLIBRARY_API int fnWin32ClassLibrary(void);

}

GetString returns a string and fnWin32ClassLibrary returns and integer. I build the code and try to call it from C# using P/Invoke. What I find is that the method name GetString is mangled but fnWin32ClassLibrary is not mangled. But I have applied extern "C" to the whole code.

Can anybody explain this to me

Regards,

Asim.



Re: Visual C++ Express Edition Doubt regarding extern "C"

Jonathan Caves - MSFT

In C++ extern "C" only applies to global functions and data - it does not apply to members of classes. Think of it as a way of saying the following is a 'C' function (or 'C' global data) as C doesn't have classes it doesn't make sense to allow it on member functions.






Re: Visual C++ Express Edition Doubt regarding extern "C"

Asim Patnaik

Hi,

Thanks for the quick reply. Now my doubt here is how do I get the unmangled name of the GetString method Is there a directive which asks the compiler not to mangle C++ class member names

Regards,

Asim.





Re: Visual C++ Express Edition Doubt regarding extern "C"

Jonathan Caves - MSFT

No: there is no way to acheive this.

Also I wouldn't go about solving the problem the way you are doing it. I would 'export' a global method which takes a pointer to the object as the first parameter. Something like:

Code Snippet

class X {

public:

void mf() { }
};

extern "C"
{

void DoCall(void* pObj)

{

reinterpret_cast<X*>(pObj)->mf();

}
}






Re: Visual C++ Express Edition Doubt regarding extern "C"

Asim Patnaik

I got it. Thank you Jonathan.