Re: Calling C written API from C# shows strange behaviour
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Tue, 31 Jan 2006 18:57:41 +0100
<jeanpaul.smit@xxxxxxxxx> wrote in message
news:1138726107.232987.216030@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|I have some strange behaviour when calling a function on a C written
| API.
| The API is part of the XCOM server by Computer Associates.
| It is very hard to find anything about this on the internet, so I hope
| to find some help here.
|
| The XCOM server documentation comes with a piece of sample code in C to
| program against the API. I have converted that sample code into a
| library and wrote a wrapper in C# to call it. (at the end the code
| should be called from a webservice which in its turn is called from
| BizTalk)
|
| The following is going on:
| 1) if I build the sample code to a C written EXE, it runs fine
| 2) if I move the sample code to a library and build a C written EXE
| calling it, it runs fine
| 3) If I build a C# wrapper calling the C written library, works only
| for one function (??)
|
| Concerning the third, the library contains 2 functions and one function
| runs fine and the other fails with "Object reference not set to an
| instance of an object".
|
| I use the following declarations in my C written API (that used to be
| the sample code):
|
| __declspec(dllexport) int __cdecl XcomSendFile(char* param1, char*
| param2, .... )
| __declspec(dllexport) char* __cdecl XcomGetCodeDescription(int code)
|
| In the C# wrapper I use the following statement to be able to access
| the C written library:
|
| [DllImport("XcomApi.dll")]
| static extern char* XcomSendFile(string param1, string param2, ....);
| [DllImport("XcomApi.dll")]
| static extern string XcomGetCodeDescription(int code);
|
| I'm pretty sure that the code that fails is identical to the code that
| runs and the libraries used are the same.
| At first I thought that there must be a difference between calling it
| via C and calling it via C#, but I'm not so sure about that anymore
| because one function works fine.
|
| Does anyone know what is going on?
|
| Thanks in advance!
|
You didn't mention which call fails, so I suppose it is this one:
static extern string XcomGetCodeDescription(int code);
here you expect a string to be returned while the C function returns a char*
(I guess). Well this is't a supported scenario for PInvoke interop, you need
to change your declaration int:
static extern IntPtr XcomGetCodeDescription(int code);
and marshal the IntPtr to a string using Marshal.PtrToStringAuto or
Marshal.PtrToStringAnsi.
Willy.
.
- References:
- Calling C written API from C# shows strange behaviour
- From: jeanpaul . smit
- Calling C written API from C# shows strange behaviour
- Prev by Date: Re: my service stops when a user logs off
- Next by Date: Re: Problem with COM in VS2005
- Previous by thread: Calling C written API from C# shows strange behaviour
- Next by thread: Re: Problem with COM in VS2005
- Index(es):
Relevant Pages
|