views:

73

answers:

3

I have a problem calling this function from a c++ DLL in c#

INT32 WINAPI PM_COM_GetText(INT32 TextId, char* pBuf, INT32 BufSize);

It writes a Text in a buffer for a given text id. I try to call it with the following c# code, but I constantly get an access violation and don't undrestand why:

public string GetText(Int32 TextId)
{
  Int32 BufSize = 256;
  StringBuilder Str = new StringBuilder(BufSize);
  PM_COM_GetText(TextId, Str, BufSize);
  return Str.ToString();
}

[DllImport("ComDll.dll", CharSet = CharSet.Ansi)]
private static extern Int32 PM_COM_GetText(Int32 TextId, StringBuilder Str, Int32 BufSize);

I don't see what's wrong, it looks to me like many other code snippets I found in the web.

Any ideas? Thanks in advance!

A: 

The StringBuilder Str in the DllImport declaration looks strange to me. I'd suggest trying to make it a plain ol' string.

sblom
that doesn't work because the DLL parmeter is an output parameter. As far as I know string only works for input parameters (and there it works fine). By the way I've tried this aleady but it didn't work
chrmue
A: 

Yep, string builder looks strange. Maybe char[]??? You should also check if actually those are not uints.

These kind if things (using int when you should use uint, lets say) will have no problem in C++ but will throw an exception in .net 2

Daniel Dolz
A: 

Maybe try using out StringBuilder Str or out string Str

Chris T