Ref string parameter is empty? - c#

I want to call in C# a function from unmanaged library with following signature:
DLL_EXPORT int xli_open(char *, int , struct t_info *);
In legacy code on Windows 7 the function is improrted as:
[DllImport(DRIVER_FILENAME, EntryPoint = "xli_open", CallingConvention = CallingConvention.Cdecl)]
public static extern int xli_open(string device, int hndl, ref t_info tInfo);
On Windows 10 I get an AccessViolationException for calling the function and I import the function as:
[DllImport(DRIVER_FILENAME, EntryPoint = "xli_open", CallingConvention = CallingConvention.Cdecl)]
public static extern int xli_open(ref string device, int hndl, ref t_info tInfo);
I don't get AccessViolationException anymore, but it seems that the function gets an empty string. Is the declaration right? And why does the pass of the ref parameter work (would string not be passed by reference anyway?)?

Assuming that you are passing the text to the function, then plain by value string is correct. The access violation is likely because of some other error. Perhaps the structure definition does not match, perhaps the calling convention is wrong. Or perhaps some other mistake, but the string argument appears to be correct.

Related

system.access.violation exception while calling c++ function from a thread in c#

i am importing c++ function from a dll to my winform c# app:
[DllImport(#"eyeWhere.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern int Eye_GetPositionS(string filename, [MarshalAs(UnmanagedType.LPArray, SizeConst = 9)] double[] sensors);
when i call the function from the constructor its working fine.
the problem is when i am calling it from a new thread opened within the
"fleck websocket server" Fleck, "onMessage" Action,
then it throws the "system.access.violation" exception.
i managed to narrow down the problem to the double array that i am passing,
it seems like the pointer to it is corrupted.
i cant find the source of the problem one thing is sure the function from the dll is working as i tested it.
function call(two stages):
open new thread within "fleck":
socket.OnMessage = message =>
{Thread locationThread = new Thread( unused => processLocation(fileName,socket,sensorsList,sensors) );
locationThread.Start();}
the actual function:
private void processLocation(string fileName, IWebSocketConnection sock, List<Sensor> sensorsList, double[] sensors)
{
int map_position = Eye_GetPositionS(fileName,sensors);
string locationString = "floor:1,mx:" + (map_position / 10000) + ",my:" + (map_position % 10000);
// send location string to user
sock.Send(LOCATION_CODE + "-" + locationString);}
the interface is:
extern "C" __declspec(dllexport) int Eye_GetPositionS(const wchar_t *fname_mob, double sensors[9], int &map_x, int &map_y)
i am not passing the two last arguments (int&) as agreed with the man who wrote the dll.
I am not passing the two last arguments (int&) as agreed with the man who wrote the DLL.
Well, there's the problem. You cannot omit parameters. It should be:
[DllImport(#"eyeWhere.dll", CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Unicode)]
public static extern int Eye_GetPositionS(
string filename,
[In, MarshalAs(UnmanagedType.LPArray, SizeConst = 9)]
double[] sensors,
ref int map_x,
ref int map_y
);
You simply cannot omit these parameters, no matter what the guy who wrote the DLL says. Perhaps he means that the parameters are really int* and you can pass nullptr.
I've used ref for these parameters, but perhaps they should be out. You presumably know which is appropriate.
Likewise I'm guessing at the intent of the sensors parameter. If the data flows out rather than in, then use Out. If the data flows in both directions, use [In, Out, ...].

What type to PInvoke for a char**

Consider the following C function:
void get_lib_version(const char **ver_string);
How do I marshall this correctly with PInvoke? The documentation says it returns a pointer to a static string. I thought this would do it:
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
public static extern int get_lib_version(StringBuilder version);
but all I get is gibberish.
The function returns a brand new C-string. The pinvoke marshaller always makes sure that the memory required to store a string that's returned by native code is released again. This will not come to a good end, surely the caller of this function is not supposed to release it. The const keyword is a strong hint that the native code will return a pointer to a string literal that's not allocated on the heap. Trying to release such a pointer will crash your program on later Windows versions, the kind that have a strict heap implementation (after XP).
You have to help to stop the marshaller from doing this. This requires you to declare the argument as a raw pointer, not a string:
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
public static extern int get_lib_version(out IntPtr version);
And you have to make the extra step to convert the pointer to a string:
public string GetLibraryVersion() {
IntPtr strptr;
get_lib_version(out strptr);
return Marshal.PtrToStringAnsi(strptr);
}
Write a little test program to verify this assumption. Call GetLibraryVersion() a billion times. If the memory usage doesn't explode then you're good.
According to this answer, when you marshal something as string, PInvoke makes all sorts of assumptions about how it's supposed to get freed. Notice that this is a const char *; it's a constant string somewhere. It never needs to be deallocated!
Apparently the way to deal with this is
Marshall as IntPtr.
Use Marshall.PtrToStringAnsi() to copy the result into a C# string.
I managed to get this to work correctly:
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern int get_lib_version(ref IntPtr version);
public static string GetLibVersion()
{
var ptrVersion = IntPtr.Zero;
get_lib_version(ref ptrVersion);
var version = Marshal.PtrToStringAnsi(ptrVersion);
return version;
}

Passing strings to c dll from C#

I have some trouble using a c dll in a c# application. The function which gives me an error is defined in the header file of the dll like this:
int __stdcall DDC_CreateFilePropertyString (DDCFileHandle file,
const char *property,
const char *value);
I added the following code in my class where I access the dll.
[DllImport("nilibddc.dll", CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
private static extern int DDC_CreateFilePropertyString(IntPtr file,
[MarshalAs(UnmanagedType.LPStr)]string property,
[MarshalAs(UnmanagedType.LPStr)]string value);
The Type DDCFileHandle is defined in the header file like this:
typedef struct _DDCFile DDCFile;
typedef DDCFile* DDCFileHandle;
There is are no additional information about the _DDCFile struct in the header file (i don't have any other files from the library im using).
Before I'm calling the function DDC_CreateFilePropertyString() I call the following function to create a file and get the file handle.
[DllImport("nilibddc.dll", CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Ansi]
private static extern int DDC_CreateFile(char[] filePath,
char[] fileType,
char[] name,
char[] description,
char[] title,
char[] author,
ref IntPtr file);
The definition in the header file looks like this.
int __stdcall DDC_CreateFile (const char *filePath,
const char *fileType,
const char *name,
const char *description,
const char *title,
const char *author,
DDCFileHandle *file);
Now always when i call the function DDC_CreateFilePropertyString it returns me an error telling me that i have some wrong parameters passed. What am i doing wrong? The library I'm using is the TDMS C API from National instruments.
Thanks for your help.
Your p/invokes are a little off. You need to use CallingConvention.Stdcall, which is the default. And for the const char* parameters you should simply declare them to be string at the C# end.
The correct C# p/invoke for DDC_CreateFile is:
[DllImport("nilibddc.dll", CharSet=CharSet.Ansi]
private static extern int DDC_CreateFile(
string filePath,
string fileType,
string name,
string description,
string title,
string author,
ref IntPtr file
);
And for DDC_CreateFilePropertyString you need this:
[DllImport("nilibddc.dll", CharSet=CharSet.Ansi)]
private static extern int DDC_CreateFilePropertyString(
IntPtr file,
string property,
string value
);
If, after fixing your code, you still receive errors when calling these functions, then you are clearly using the library incorrectly. And that's beyond the scope of this question. Consult the documentation, and/or seek support from the library vendor.

Invoke method for dll in WindowsForms

I have a dll which contains this function:
int __stdcall PrnText(char *printtext);
In Windows Forms i have this code to invoke the dll:
[DllImport("Printing.dll", EntryPoint = "PrnText", CharSet = CharSet.Ansi)]
public static extern int PrnText(char *printtext);
When i call the function in C# code i get an error like this : " cannot cast string to char*
PrnText("Hello World");
What parameter should i give to PrnText() to make it work?
Later edit:
Parameter: printtext
pointer to string containing text to be printed
The CLR knows how to convert a string to an unmanaged char* at runtime. You should use a signature which accepts a string, as such:
public static extern int PrnText(string printtext);
Note that this will work only if the parameter is input only.

Wrapper c# using unmanaged dll

I am making a wrapper to read TDM and TDMS files but i have a problem
[DllImport(lib, CharSet = CharSet.Auto)]
static extern int DDC_OpenFileEx(
[MarshalAs(UnmanagedType.LPStr)]
string filePath,
[MarshalAs(UnmanagedType.LPStr)]
string fileType,
int read_only,
ref long file);
works fine but
[DllImport(lib, CharSet = CharSet.Auto, SetLastError = true)]
static extern int DDC_GetNumChannelGroups(long file,
[MarshalAs(UnmanagedType.U4)]
ref int numChannelGroups);
int numGru = 0;
errCode = ReadTDM.DDC_GetNumChannelGroups(file,ref numGru);
System.Console.WriteLine("Error Code {0} GetNumChannelGroups", errCode);
gives an error -6202, // An invalid argument was passed to the library.
i have tried ref uint, uint * (unsafe), UIntPtr. The def from .h file
int __stdcall DDC_GetNumChannelGroups (DDCFileHandle file,unsigned int *numChannelGroups);
the second parametr is the problem.
it seems that unsigned int* != uint.
Does anyone have an idea how to call this function form the dll?
http://forums.ni.com/ni/board/message?board.id=60&thread.id=11821
It is the 1st argument that's declared wrong. That throws off the stack frame and prevents the unmanaged code from properly reading the pointer for the 2nd argument. "long" is 64-bits, DDCFileHandle is almost certainly a pointer, 32-bits on a 32-bit operating system.
Change the argument declaration to IntPtr. You'll also need to change the declaration of the function that returns that handle.

Categories

Resources