Pinvoke and decorated names

T

Tim Johnson

From C# I tried to P/Invoke a C-style function in a simple 1-function DLL I
created with evc++4.0. It fails with MissingMethodException. I finally
figured out it's because the exported function name is decorated by the
compiler/linker, so instead of "Myfcn" it's ?Myfcn@@YAHZZ. If I use
EntryPoint it works:

[DllImport("mydll.dll", EntryPoint="?Myfcn@@YAHXZ")]
public static extern int Myfcn();

I can also use EntryPoint="#1" (the ordinal number).

My question is: how can I get the compiler to NOT decorate this simple
C-style function? My real goal is to be able to create flat C-style
wrappers for a legacy C++ class-based dll we havehere, but I'd really like
to use the standard names of my flat functions, not either of the above
tricks.

Tim Johnson
 
A

Alex Feinman [MVP]

Tim Johnson said:
From C# I tried to P/Invoke a C-style function in a simple 1-function DLL
I
created with evc++4.0. It fails with MissingMethodException. I finally
figured out it's because the exported function name is decorated by the
compiler/linker, so instead of "Myfcn" it's ?Myfcn@@YAHZZ. If I use
EntryPoint it works:

[DllImport("mydll.dll", EntryPoint="?Myfcn@@YAHXZ")]
public static extern int Myfcn();

I can also use EntryPoint="#1" (the ordinal number).

My question is: how can I get the compiler to NOT decorate this simple
C-style function? My real goal is to be able to create flat C-style
wrappers for a legacy C++ class-based dll we havehere, but I'd really like
to use the standard names of my flat functions, not either of the above
tricks.


By prefixing the function definition in your C++ code(.h file ) with
extern "C"

E.g.
extern "C
int WINAPI Myfcn();
 
T

Tim Johnson

I tried it like this:

extern "C" __declspec (dllexport) int Myfcn();

and Dumpbin now reports it as just "Myfcn" (not decorated). However, I
still get the MissingMethod exception when I declare it like this:

[DllImport("Mydll.dll")]
public static extern int Myfcn();

I also tried declaring it as "_Myfcn" in the DllImport statement, but same
result.

--
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625

Alex Feinman said:
Tim Johnson said:
From C# I tried to P/Invoke a C-style function in a simple 1-function DLL
I
created with evc++4.0. It fails with MissingMethodException. I finally
figured out it's because the exported function name is decorated by the
compiler/linker, so instead of "Myfcn" it's ?Myfcn@@YAHZZ. If I use
EntryPoint it works:

[DllImport("mydll.dll", EntryPoint="?Myfcn@@YAHXZ")]
public static extern int Myfcn();

I can also use EntryPoint="#1" (the ordinal number).

My question is: how can I get the compiler to NOT decorate this simple
C-style function? My real goal is to be able to create flat C-style
wrappers for a legacy C++ class-based dll we havehere, but I'd really like
to use the standard names of my flat functions, not either of the above
tricks.


By prefixing the function definition in your C++ code(.h file ) with
extern "C"

E.g.
extern "C
int WINAPI Myfcn();
 
A

Alex Feinman [MVP]

The declaration is correct. Now you need to make sure that the dll is in the
same directory as the app and that the architecture for which you compiled
it matches the platform - x86 for emulator and ARM for the device

--
Alex Feinman
---
Visit http://www.opennetcf.org
Tim Johnson said:
I tried it like this:

extern "C" __declspec (dllexport) int Myfcn();

and Dumpbin now reports it as just "Myfcn" (not decorated). However, I
still get the MissingMethod exception when I declare it like this:

[DllImport("Mydll.dll")]
public static extern int Myfcn();

I also tried declaring it as "_Myfcn" in the DllImport statement, but same
result.

--
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625

Alex Feinman said:
Tim Johnson said:
From C# I tried to P/Invoke a C-style function in a simple 1-function DLL
I
created with evc++4.0. It fails with MissingMethodException. I
finally
figured out it's because the exported function name is decorated by the
compiler/linker, so instead of "Myfcn" it's ?Myfcn@@YAHZZ. If I use
EntryPoint it works:

[DllImport("mydll.dll", EntryPoint="?Myfcn@@YAHXZ")]
public static extern int Myfcn();

I can also use EntryPoint="#1" (the ordinal number).

My question is: how can I get the compiler to NOT decorate this simple
C-style function? My real goal is to be able to create flat C-style
wrappers for a legacy C++ class-based dll we havehere, but I'd really like
to use the standard names of my flat functions, not either of the above
tricks.


By prefixing the function definition in your C++ code(.h file ) with
extern "C"

E.g.
extern "C
int WINAPI Myfcn();
 
T

Tim Johnson

That was it - I had a stray copy of the dll in a wrong place. Dang these
shifty project settings!

Thanks for the help.

--
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625

Alex Feinman said:
The declaration is correct. Now you need to make sure that the dll is in the
same directory as the app and that the architecture for which you compiled
it matches the platform - x86 for emulator and ARM for the device

--
Alex Feinman
---
Visit http://www.opennetcf.org
Tim Johnson said:
I tried it like this:

extern "C" __declspec (dllexport) int Myfcn();

and Dumpbin now reports it as just "Myfcn" (not decorated). However, I
still get the MissingMethod exception when I declare it like this:

[DllImport("Mydll.dll")]
public static extern int Myfcn();

I also tried declaring it as "_Myfcn" in the DllImport statement, but same
result.

--
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625

Alex Feinman said:
From C# I tried to P/Invoke a C-style function in a simple 1-function DLL
I
created with evc++4.0. It fails with MissingMethodException. I
finally
figured out it's because the exported function name is decorated by the
compiler/linker, so instead of "Myfcn" it's ?Myfcn@@YAHZZ. If I use
EntryPoint it works:

[DllImport("mydll.dll", EntryPoint="?Myfcn@@YAHXZ")]
public static extern int Myfcn();

I can also use EntryPoint="#1" (the ordinal number).

My question is: how can I get the compiler to NOT decorate this simple
C-style function? My real goal is to be able to create flat C-style
wrappers for a legacy C++ class-based dll we havehere, but I'd really like
to use the standard names of my flat functions, not either of the above
tricks.


By prefixing the function definition in your C++ code(.h file ) with
extern "C"

E.g.
extern "C
int WINAPI Myfcn();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top