Re: Calling C written API from C# shows strange behaviour

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




<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.



.



Relevant Pages

  • Calling C written API from C# shows strange behaviour
    ... I have some strange behaviour when calling a function on a C written ... The API is part of the XCOM server by Computer Associates. ... The XCOM server documentation comes with a piece of sample code in C to ... runs and the libraries used are the same. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Future FASMLIB website
    ... > The big problem with calling libc code is that most assembly ... > programmers reject that idea. ... One has no choise but calling the win api, in any case dealing with IO. ...
    (alt.lang.asm)
  • Re: ExitWindowsEx function not working.
    ... you should always check the API return value ... to determine if the API calling fails. ... As Int32, ByVal dwreserved As Int32) As Int32 ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Does fopen fclose implement open and close internally??
    ... Does fopen fclose implement open and close ... If it is so how does the overhead of calling low-level ... precompiled libraries. ... C- Programmer has to only worry about their code conforming to C-std ...
    (comp.unix.programmer)
  • Re: C# program calling a C++ DLL
    ... I found a sample code in C++, but to make it work I had to use the .lib ... public static extern unsafe long myFunction1; ... Doing a quick check calling it from C++ ... > question but you never know), is your DLL in the correct directory? ...
    (microsoft.public.dotnet.languages.csharp)