473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calling C++ dll from C# using a function with char** as param with DllImport

Hi,

I'm trying to call a C++ function that has a char** as a parameter from C# code using DllImport. After googling around and finding some threads I haven't been able to solve my problem. This is what I have...

C++ side

void populateRequiredIndexNames(char** names);


C# side
[DllImport("QRMAPIDummyImpl.dll", CharSet = CharSet.Auto)]
private static extern void populateFieldCategories(out IntPtr categories);



public string[] FieldCategories
{
get
{
int num = NumberOfFieldCategories;
string[] categories = new string[num];
IntPtr ptr;
populateFieldCategories(out ptr); <---- FAILS HERE

for (int i = 0; i < num; i++ )
{
categories[i] = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(ptr, i*IntPtr.Size));
}

return categories;
}
}


It gives me an System.AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

The C++ team is trying to pass an array of C-strings, but I don't seem to be able to get that. Other methods work fine (ones returning primitive types or char*) but the method with char** doesn't want to work.

Is it ok for them to pass a char** in the C++ function or should they pass a char*** if they want to give me an array of C-strings?

Any help would be greatly appreaciated.

Thx
Jul 26 '07 #1
3 6357
weaknessforcats
9,208 Expert Mod 8TB
The C++ function cannot return an array of strings. What it can do is return the address of the first string.

If this is an array of char* strings, then the C++ function can return the address as of element 0 as a char**. However, it also needs to return the number or strings in the array. Otherwise, the caller has no idea how many strings there are.

Possible solutions:
1)The last string in the array is guraranteed empy. The caller processes until it encounters an empty string
2) Return the number of strings in ther array.

I recommend the second choice and return a struct:

Expand|Select|Wrap|Line Numbers
  1. struct StringArray
  2. {
  3.     char** str;    //address of first string
  4.     unsigned int;  //number of strings.
  5. };
  6.  
In this case str[3] is the 4th string.

This does not solve problems of ASCII/UNICODE where you may need to use TCHAR mappings in C++. Now that I think of it, I don't believe you are supposed to use char* strings in C++ or C# where Microsoft code is concerned. I remember reading something about that.

An access violation exception is almost certainly an uninitialized pointer onthe C++ side unless this:
IntPtr ptr;
requires some kind of initial value before being used. Not being a C# person, this is definitely a bug in C++.

Also:
string[] categories = new string[num];
is on the C# side. How do you know the C++ folks have num strings??

If they have 4 and num is 10, you die since your load loop runs to num.
Jul 26 '07 #2
Thanks for the suggestions I'll give it a try, there's another method that return the number of categories, I don't have a problem reading that one and I'm using it to initialize the array.

I wonder if it would be easier to create a managed C++ dll that wraps the unmanaged one and then use the managed C++ dll to talk to C#. Do you know if the manipulation of char*, char** and structs in managed C++ can make it easier for me and just pass to C# the equivalents like for example instead of having to deal with char* in C#, the managed C++ dll will take care of it and pass to C# a wstring that C# can use directly?

It seems that PInvoke is usefull for simple things but it seems it gets much more cumbersome when you need to pass strucs and arrays of c-string.
Jul 26 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You are probably better off using Managed C++ and inside that code have sections of unmanaged C++ where you need them.
Jul 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: OzgurGul | last post by:
int main(){ /* I have a function :) and I want to use it but I dunno how to call it... (I have trouble that pointers) please help me... mmm... thanks for all advance!! */
9
by: Andrew Cumming | last post by:
I am having trouble using Platform Invoke to call C++ DLLs from C# windows applications. The DLLs seem to load, but I get run-time errors to the effect that the named entry points cannot be found....
3
by: Webdiyer | last post by:
I want to integrate SecurID two-factor authentication system of the RSASecurity.inc into our asp.net application,but I get into trouble when trying to call the API functions of the ACE Agent,I got...
1
by: | last post by:
Hey all, Just starting to play with calling DLL's outside the .NET sandbox and I'm hoping for some help with the following: The function prototypes: public static extern long ...
2
by: Wilfried Mestdagh | last post by:
Hi, If I make a DLL in Delphi (5..7, so not NET) to manipulate strings, then I assume I have to do something like this: public static extern TestProc(String C); and in the Delphi DLL:
17
by: Bill Grigg | last post by:
I have been successfully calling DLL's using VC++ 6.0 and also using VC++7.1 (.NET). I only mention this because I have never felt comfortable with the process, but nonetheless it did work....
3
by: Dave Cullen | last post by:
How do I declare and call a function in a dll with VB.NET? The prototype for my function is extern "C" BOOL PASCAL EXPORT CreateProxCodes(char *formatpath, char *formatname, struct CPSWorkOrder...
2
by: JoeB | last post by:
Hi Trying to call the following (c++) dll: void getVersionInfo( long p_lIndex, char* p_cVersionNumber, int p_iVersBufSize, char* p_cDescription, int p_iDescBufSize, BOOL& p_bCompulsory ); ...
11
by: briankirkpatrick | last post by:
Forgive me if my post seems a little amateurish... I'm requesting assistance from some of you smart folks out there to get the managed calls write that meet the specification in the esa.h for...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.