extern "C" returns error

  • Thread starter Thread starter Lio
  • Start date Start date
L

Lio

Hi

I am using the MFC DLL project in .NET platform and i try to use [extern
"C"] infront of the function decleration to get a clean function name in the
DLL.
this is my simple code:

1 class CTestDllApp : public CWinApp
2 {
3 public:
4 CTestDllApp();
5 extern "C" __declspec(dllexport) int MyFunction(int);
6 // Overrides
7 public:
8 virtual BOOL InitInstance();
9 DECLARE_MESSAGE_MAP()
10 };

at line 5 i get error for the 'string' decleration
(g:\TestDll\TestDll\TestDll.h(21): error C2059: syntax error : 'string')

What is the problem???

thanks

Lio
 
Lio said:
Hi

I am using the MFC DLL project in .NET platform and i try to use
[extern "C"] infront of the function decleration to get a clean
function name in the DLL.
this is my simple code:

1 class CTestDllApp : public CWinApp
2 {
3 public:
4 CTestDllApp();
5 extern "C" __declspec(dllexport) int MyFunction(int);
6 // Overrides
7 public:
8 virtual BOOL InitInstance();
9 DECLARE_MESSAGE_MAP()
10 };

at line 5 i get error for the 'string' decleration
(g:\TestDll\TestDll\TestDll.h(21): error C2059: syntax error :
'string')

What is the problem???

Umm, you have a C++ class and then you say that one of its methods has a C
name - doesn't that seem odd to you? The C++ mangled name (sorry 'decorated'
name) has information in it about the class, the parameters and return value
which is used by the C++ compiler when trying to write code that calls the
class. If you could make the method extern "C" then this information would
not be available to the compiler.

You are trying to export the method from the DLL, but this is an ugly way to
do it. You can export the mangled name and import that into C# with
[DllImport] but you have the problem of the implicit first parameter 'this'
pointer.

Its far better to write some managed C++ that has a public managed class
with a public MyFunction that creates a CTestDllApp unmanaged object and
delegates the call to that object's methods.

Richard
 
If you are trying to export a C++ method from your dll as a C function -
you'll have to wrap it. The C/C++ Users Journal this month has a good
article on how to do this in the Conversations article by Herb Sutter and
Jim Hyslop.

Richard Grimes said:
Lio said:
Hi

I am using the MFC DLL project in .NET platform and i try to use
[extern "C"] infront of the function decleration to get a clean
function name in the DLL.
this is my simple code:

1 class CTestDllApp : public CWinApp
2 {
3 public:
4 CTestDllApp();
5 extern "C" __declspec(dllexport) int MyFunction(int);
6 // Overrides
7 public:
8 virtual BOOL InitInstance();
9 DECLARE_MESSAGE_MAP()
10 };

at line 5 i get error for the 'string' decleration
(g:\TestDll\TestDll\TestDll.h(21): error C2059: syntax error :
'string')

What is the problem???

Umm, you have a C++ class and then you say that one of its methods has a C
name - doesn't that seem odd to you? The C++ mangled name (sorry 'decorated'
name) has information in it about the class, the parameters and return value
which is used by the C++ compiler when trying to write code that calls the
class. If you could make the method extern "C" then this information would
not be available to the compiler.

You are trying to export the method from the DLL, but this is an ugly way to
do it. You can export the mangled name and import that into C# with
[DllImport] but you have the problem of the implicit first parameter 'this'
pointer.

Its far better to write some managed C++ that has a public managed class
with a public MyFunction that creates a CTestDllApp unmanaged object and
delegates the call to that object's methods.

Richard
 
Back
Top