473,385 Members | 1,958 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,385 software developers and data experts.

Calling unmanaged C++ dll function with char**

Hi,

I'm calling an unmanaged C++ dll from C#, everything was fine until I got stuck in one of the methods that goes like this...

C++ part
void populateFieldCategories(char** categories)

I have tried using something like this in C# to get the array of strings but doesn't work...
private static extern void populateFieldCategories([MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPArray)] byte[][] categories)
with an error like "There's no marshaling support for nested arrays"

and I have also tried something like

private static extern void populateFieldCategories([MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] categories)
but fails saying "Attempted to read or write protected memory"

I don't know what else to try, if someone want to pass a char** from C++ how can you get the information in C#?

Thanks
Jul 25 '07 #1
6 1967
One thing that I forgot to pot is that this C++ dll is in-house so if calling a function with a char** parameter is not possible and you can give me suggestions I can tell the other team to use other data types or another construct, but I'd like suggestions.

Thanks
Jul 26 '07 #2
Plater
7,872 Expert 4TB
I'm a little rusty but isn't a char** a pointer to an array of characters? Or is it a pointer to an array of array of characters?
I think it would transpose to C# better if you could use a char[] of some sort. You really need to avoid pointers though in managed code, you never know what you'll get.
Jul 26 '07 #3
I'm a little rusty but isn't a char** a pointer to an array of characters? Or is it a pointer to an array of array of characters?
I think it would transpose to C# better if you could use a char[] of some sort. You really need to avoid pointers though in managed code, you never know what you'll get.
What they are trying to past is an array of strings, but they definied the dll interface as char**. Hope this helps
Jul 26 '07 #4
Now I'm trying with something different, like this but it doesn't work for me either...



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.



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?
Jul 26 '07 #5
TRScheel
638 Expert 512MB
Try putting it into an unsafe block
Jul 26 '07 #6
Try putting it into an unsafe block
Thanks, I'll try that
Jul 26 '07 #7

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

Similar topics

7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
4
by: Angel | last post by:
I'm trying to call a DLL function that receives as parameter a user-defined structure created by the company that made the dll. When I call the function from my main form, I call...
3
by: Angel | last post by:
Hello again (and again, and again...) I think I'm getting closer to solving my initial problem of calling unmanaged code. I managed to call the functions with user-defined structs w/o getting any...
8
by: Johann Blake | last post by:
I need to call a C DLL function. The first parameter expects a pointer to a long. It returns a value at the address of the pointer. The second parameter expects a pointer to a pointer. It creates...
0
by: sklett | last post by:
I'm having a really hard time with wrapping an unmanaged class with a managed class, then calling that managed class from my C# code. I will show you the three pieces, then explain: --------...
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....
1
by: slugster | last post by:
Hi, i originally posted this via another portal, but after giving it time to propagate it still hasn't shown up. My apologies for the multiposting. This might be a very simple question, but i...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
1
by: Leftie | last post by:
Folks, I'm trying to call an unmanaged function from VB.NET and keep getting "Object reference not set to an instance of an object" error. The code that i wrote can be found at:...
1
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.