使用DLLImport与char **和float ** [英] Using DLLImport with char** and float**

查看:213
本文介绍了使用DLLImport与char **和float **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用DLLImport在C#程序中使用DLL文件。
我在DLL中具有以下功能:

  int method(char * method,char ** params,int n_params,float ** res_arr,int * n_res); 

函数调用应该是这样的:

  method =method1
char ** = {param1 = 1,param2 = 2}
n_params = 2
res_arr = DLL函数分配一个数组并指向它
n_res = DLL函数设置为结果数

有一个独立的函数来释放浮动**。

我当前在C#中的代码是这样的:

  private static extern int method(string method,ref IntPtr params,Int32 n_params,ref IntPtr res_arr,IntPtr n_res); 

我刚刚接触C#(而且我的C知识有点缺乏)我没有弄清楚如何调用这个功能(已经在我的键盘上玩了两天)。有人可以给我一个如何做这个例子,如何调用函数?



我的主要问题是如何处理 char ** float ** ,我不知道是否在声明中是正确的指针类型,不知道我是应该创建并发送我的 char ** 到函数。



值得注意的是,我可能不会更改任何内容在DLL文件中。



编辑

这是释放结果数组的函数的描述: p>

free_results(float * res_arr)



EDIT2
我现在可以调用该方法并获取值,现在我的问题是我在访问float值时似乎有问题。如我所说,我使用 Marshal.Copy()这样:

  [DllImport(libs\\\myDll.dll,CallingConvention = CallingConvention.Cdecl)] 
private static extern int method(string method,string [] params,Int32 n_params,out IntPtr res_arr,ref int n_res );

IntPtr res_arr = IntPtr.Zero;
int n_res = 0;
string [] s = new string [] {param1};

方法(analyze,s,s.Length,out res_arr,ref n_res);

float [] f_res = new float [n_res];
Marshal.Copy(res_arr,f_res,0,n_res);

问题是我只是在浮动向量中看到垃圾值。例如,在一种情况下,我应该得到 100.0 ,但是要获得 15.0 3840.0 ,它告诉我,当我复制时使用指针错误或有其他的东西。 DLL中的代码正常工作,因为还有另一个用C编写的程序,它得到正确的值。感觉就像我做了一个指针的浮动,而不是它指向什么。

解决方案

这是解决我的初始问题(由Hans Passant建议):

  [DllImport(libs\\\myDll.dll,CallingConvention = CallingConvention .Cdecl)] 
private static extern int method(string method,string [] params,Int32 n_params,out IntPtr res_arr,ref int n_res);

IntPtr res_arr = IntPtr.Zero;
int n_res = 0;
string [] s = new string [] {param1};

方法(analyze,s,s.Length,out res_arr,ref n_res);

float [] f_res = new float [n_res];
Marshal.Copy(res_arr,f_res,0,n_res);

我的第二个问题是float数组给出垃圾值,这是我完全屁股的结果帽子。在DLL中还有一个函数,在使用方法(...)之前必须调用它,以使它具有值来处理。添加完这个调用后,一切都很完美。


I'm trying to use a DLL file in a C# program using DLLImport. I have the following function in the DLL:

int method(char* method, char** params, int n_params, float** res_arr, int* n_res);

Function call should be something like this:

method   = "method1"
char**   = {"param1=1", "param2=2"}
n_params = 2
res_arr  = the DLL function allocates an array and points this to it
n_res    = the DLL function sets to the number of results

There is a seperate function for freeing the float**.
My current code in C# is this:

private static extern int method(string method, ref IntPtr params, Int32 n_params, ref IntPtr res_arr, IntPtr n_res);

I'm new to C# (and my C knowledge is a bit lacking) and can for the life of me not figure out how to call this function (been faceplanting my keyboard for two days). Could someone give me an example of how this should be done and how to call the function?

My main problem is what to do with the char** and float**, I don't know if it's the correct pointer types in the declaration and don't know how I'm supposed to create and send my char** to the function.

Worth noting is that I may not change anything in the DLL file.

EDIT
This is the description of the function which releases the result array:

free_results(float* res_arr)

EDIT2 I can now call the method and I get values back, my problem now is that I seem to have a problem accessing the float values. As suggested I'm using Marshal.Copy() like this:

[DllImport("libs\\myDll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int method(string method, string[] params, Int32 n_params, out IntPtr res_arr, ref int n_res);

IntPtr res_arr = IntPtr.Zero;  
int n_res = 0;
string[] s = new string[] { "param1" };  

method("analyze", s, s.Length, out res_arr, ref n_res);  

float[] f_res = new float[n_res];
Marshal.Copy(res_arr, f_res, 0, n_res);

The problem is I only seem to get rubbish values in the float vector. For example in one case i should get 100.0 but get either 15.0 or 3840.0, which tells me that either I'm using the pointer wrong when copying or there is something else fishy. The code in the DLL is working as it should since there is another program written in C which gets the correct values. It feels like I making a float of the pointer and not what it points at.

解决方案

This is the code that solved my initial problem (as suggested by Hans Passant):

[DllImport("libs\\myDll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int method(string method, string[] params, Int32 n_params, out IntPtr res_arr, ref int n_res);

IntPtr res_arr = IntPtr.Zero;  
int n_res = 0;
string[] s = new string[] { "param1" };  

method("analyze", s, s.Length, out res_arr, ref n_res);  

float[] f_res = new float[n_res];
Marshal.Copy(res_arr, f_res, 0, n_res);

My second problem, where the float array gave rubbish values, were a result of me being a complete ass hat. There is another function in the DLL which has to be called before using method(...), in order for it to have values to process. After adding that call everything worked perfectly.

这篇关于使用DLLImport与char **和float **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆