Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing DLLImport with char** and float**
    primarykey
    data
    text
    <p>I'm trying to use a DLL file in a C# program using DLLImport. I have the following function in the DLL:</p> <pre><code>int method(char* method, char** params, int n_params, float** res_arr, int* n_res); </code></pre> <p>Function call should be something like this:</p> <pre><code>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 </code></pre> <p>There is a seperate function for freeing the float**.<br> My current code in C# is this:</p> <pre><code>private static extern int method(string method, ref IntPtr params, Int32 n_params, ref IntPtr res_arr, IntPtr n_res); </code></pre> <p>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?</p> <p>My main problem is what to do with the <code>char**</code> and <code>float**</code>, 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 <code>char**</code> to the function.</p> <p>Worth noting is that I may not change anything in the DLL file.</p> <p><strong>EDIT</strong><br> This is the description of the function which releases the result array:</p> <p><code>free_results(float* res_arr)</code></p> <p><strong>EDIT2</strong> 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 <code>Marshal.Copy()</code> like this:</p> <pre><code>[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); </code></pre> <p>The problem is I only seem to get rubbish values in the float vector. For example in one case i should get <code>100.0</code> but get either <code>15.0</code> or <code>3840.0</code>, 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload