2

I have a DLL file that is written in C. I tried to use it in managed code but some how my function is not working properly. Here is the C code.

int preProcessImagesC (char *p_trainingFilePath,
                       char **p_vecImageFilesOrDirs);  

This function is working fine.

Managed code :

unsafe private static extern int preProcessImagesC(
    //Works perfact 
    String p_trainingFilePath,
    //char** thise parameter is taking junk values , String Array is not working
    [MarshalAs(UnmanagedType.SafeArray)] ref String[] p_vecImageFilesOrDirs);

Only first parameter is working properly. What should I use for char **p_vecImageFilesOrDirs parameter in managed code. Please help me to write compatible code in C#.

5
  • 2
    It is not possible to answer this question with the information here. We don't know the flow of data. What exactly is p_vecImageFilesOrDirs? Is it an array of strings, passed in to the native code? Or is is a pointer to string, passed by reference, to allow the native code to return a single string to the caller? Or is it something else? Likewise what is p_trainingFilePath? In or out? And what about calling convention? Where do you specify that? And why use unsafe? Finally, please format the code. The code looks like it has just been puked in there! Lay it out neatly. Commented Apr 10, 2013 at 9:30
  • p_vecImageFilesOrDirs: [Input parameter] An array of strings (null-terminated strings) of all the image files or directories that contain the input images that are to be processed. Calling function: int iResult = preProcessImagesC(p_trainingFilePath, ref p_vecImageFilesOrDirs, ); Commented Apr 10, 2013 at 12:01
  • The function will ignore any directories that does not exist, and will return -1 if none of the directories/files exist. Commented Apr 10, 2013 at 12:08
  • Sounds like MD's answer is correct Commented Apr 10, 2013 at 12:13
  • How does the callee obtain the length of the array? Commented Apr 10, 2013 at 15:40

2 Answers 2

4

Did you try:

private static extern int preProcessImagesC(
    string p_trainingFilePath,
    string[] p_vecImageFilesOrDirs
);

The marshaller automatically uses:

[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPTStr)]

that is what you need.

You should be careful about this, because there is no way that your unmanaged code can determine the actual size of the passed array. You have to pass the real size of the array to the unmanaged function as another parameter, or use a fixed size in both places.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the replay. I have used String[] but string[] p_vecImageFilesOrDirs is not working so I used ref Byte[] p_vecImageFilesOrDirs but still it works for the one item in array need solution for multiple items.
+1 The code in this answer works correctly if @user's first comment to the question is accurate.
@user2181754 It is strange that this works for a single string but not for a string array. Can you test it with a simple function? Try with a single string array parameter.
@MD.Unicorn I have tested it. The code works perfectly. The explanation is that user2181754 has described the interface correctly. But a DLL that receives an array of null-terminated strings, will receive the data correctly with the code in this answer. The question is wrong.
unless user2181754's DLL requires the array of strings to be terminated (like ANSI C requires of main's argv) with a pointer to NULL, to tell it that it's at the end of the list?
|
-2

A string is already an array of characters.
Thus you should be fine with

ref String p_vecImageFilesOrDirs

You are currently copying a reference to array of array of characters.

1 Comment

Byte[] is working but still need solution for multiple items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.