0

I import a C++ dll into a C# project and, on a x64 machine, on debug mode, PInvoke complains that managed signature does not match the unmanaged target signature.

C++:

void _Foo(signed long int x);

C#:

[DllImport("foo.dll", EntryPoint="_Foo"]
public static extern void Foo(int x)

Replacing int in the C# code with IntPtr or Int64 didn't solve the problem. Any suggestions?

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
anon
  • 11
  • 1
  • 2
    What is the exact error coming from pinvoke? I think you're asking the wrong question here (I answered the question you asked but it's not going to help you). – Larry Osterman Mar 24 '11 at 14:57
  • Exactly Larry, I had a similar answer yet got marked down. >:( – anothershrubery Mar 24 '11 at 15:06
  • 2
    My best guess is that this is actually compiled as 32-bit. The EntryPoint property is wrong, it is __Foo (two underscores). CallingConvention is missing too. Not posting the *exact* error message was a mistake. – Hans Passant Mar 24 '11 at 15:26

2 Answers2

3

It's System.Int32. Also known as "int".

Larry Osterman
  • 16,086
  • 32
  • 60
  • 1
    And yes, I read the part of the question that said that pinvoke complained about the signature mismatch. If it complained, it's not complaining about the "int" part. – Larry Osterman Mar 24 '11 at 14:57
  • 1
    No - the C# "long" type is System.Int64. But he asked about the C++ long type which is a 32bit number (on most 32bit operating systems and all Windows systems). – Larry Osterman Mar 25 '11 at 05:36
0

A long int in C++ == int in C#. Both are 4 bytes long. A long long in C++ == long in C#. (8 bytes).

As Larry above says, if it is picking up a type mismatch, it is not because of the int.

anothershrubery
  • 20,461
  • 14
  • 53
  • 98
  • Why has this been marked down?! It is stating fact and points towards a different problem than the one assumed by the original poster. Mark downs should be accompanied by a mandatory comment. So frustrating. – anothershrubery Mar 24 '11 at 15:03
  • I was getting to it :). Had to walk out of the office. It's because of your over-zealous claim that in C++ a long int is 4 bytes long. This is not the case. until you attach a specific platform to those numbers, this answer is not a "fact" and is indeed wrong. – San Jacinto Mar 24 '11 at 15:06
  • 1
    Strange I always went under the assumption that it was always the same. Can't find any evidence to go against this but did find http://jk-technology.com/c/inttypes.html and http://msdn.microsoft.com/en-us/library/s3f49ktz(v=VS.100).aspx which both define a long int as 4 bytes. – anothershrubery Mar 24 '11 at 15:11
  • If you're talking specifically about MS's C++ compiler documented on MSDN, then your statement is correct. If you say "C++" in the general term, you're wrong. You just need more specificity. – San Jacinto Mar 24 '11 at 15:22
  • The other document doesn't specify which target platform it is talking about. And various other sources I have searched for also do not specify particular platforms, but I'll bow to your greater knowledge, I am a .Net developer and only have brief crossings with C++. Maybe you could cite some documentation which states differences of types across platforms? – anothershrubery Mar 24 '11 at 15:29
  • I don't have access to the C++ standard, but it's in there. You don't get size promises of the nature that you get in Java and .NET. see http://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c/271132#271132 for an example. – San Jacinto Mar 24 '11 at 15:34
  • Actually, read the document that you posted http://jk-technology.com/c/inttypes.html#int. It makes my point. – San Jacinto Mar 24 '11 at 16:25