473,385 Members | 2,162 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 DLL function from C# and char** parameter type

I'm trying to call a dll function from C# which has the following form:

int some_function(int count, char **arg1, char **arg2)

Which parameter type I need to use in C# for C++ char** type? I tried
byte[][] (array of byte[]) but I get error "There is no marshaling support
for nested arrays."
regards
Tomaz
Jan 12 '08 #1
11 5629
Tomaz,

Are the strings being read from, or are they being written to? If they
are being read from, you should be able to pass it as an array of strings,
and it should work.

If they are being written to, then you will have to declare the
parameters as IntPtrs, allocate the memory for them, and then marshal the
data yourself (don't forget to unallocate the memory).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Siol" <un*****@abc.comwrote in message
news:zL*******************@news.siol.net...
I'm trying to call a dll function from C# which has the following form:

int some_function(int count, char **arg1, char **arg2)

Which parameter type I need to use in C# for C++ char** type? I tried
byte[][] (array of byte[]) but I get error "There is no marshaling support
for nested arrays."
regards
Tomaz
Jan 12 '08 #2
Siol wrote:
I'm trying to call a dll function from C# which has the following
form:

int some_function(int count, char **arg1, char **arg2)

Which parameter type I need to use in C# for C++ char** type? I tried
byte[][] (array of byte[]) but I get error "There is no marshaling
support for nested arrays."
Probably something like:

[DllImport(yourdllname, CharSet=CharSet.Ansi)]
public static extern int some_function(int count,
string[] arg1, string[] arg2);

--
Rudy Velthuis http://rvelthuis.de

"Everyone is a genius at least once a year; a real genius has his
original ideas closer together."
-- Georg Lichtenberg (1742-1799)
Jan 13 '08 #3
The strings are input parameters to the function (init parameters).

regards
Tomaz
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:D4**********************************@microsof t.com...
Tomaz,

Are the strings being read from, or are they being written to? If they
are being read from, you should be able to pass it as an array of strings,
and it should work.

If they are being written to, then you will have to declare the
parameters as IntPtrs, allocate the memory for them, and then marshal the
data yourself (don't forget to unallocate the memory).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Siol" <un*****@abc.comwrote in message
news:zL*******************@news.siol.net...
>I'm trying to call a dll function from C# which has the following form:

int some_function(int count, char **arg1, char **arg2)

Which parameter type I need to use in C# for C++ char** type? I tried
byte[][] (array of byte[]) but I get error "There is no marshaling
support for nested arrays."
regards
Tomaz

Jan 14 '08 #4
Tomaz Koritnik wrote:
The strings are input parameters to the function (init parameters).
Then (string[] arg1, string[] arg2) will do.

--
Rudy Velthuis http://rvelthuis.de

"Analyzing humor is like dissecting a frog. Few people are
interested and the frog dies of it."
-- E. B. White (1899-1985)
Jan 14 '08 #5
I tried with string[] but as soon as I call the function, whole application
terminates instantly with no error message. I haven't tried with
CharSet=CharSet.Ansi yet.

from MySQL specification the dll function looks like this: int
mysql_library_init(int argc, char **argv, char **groups)

my implementation:

[DllImport("libmysqld.dll")]
private static extern int mysql_server_init(int argc, string[] argv,
string[] groups);
{
string[] argv = new string[1];
argv[0] = "mysql_test";
string[] groups = new string[2];
groups[0] = "libmysqd_server";
groups[1] = "libmysqd_client";
mysql_server_init(0, argv, groups);
}

regards

Tomaz


"Rudy Velthuis" <ne********@rvelthuis.dewrote in message
news:xn****************@news.microsoft.com...
Tomaz Koritnik wrote:
>The strings are input parameters to the function (init parameters).

Then (string[] arg1, string[] arg2) will do.

--
Rudy Velthuis http://rvelthuis.de

"Analyzing humor is like dissecting a frog. Few people are
interested and the frog dies of it."
-- E. B. White (1899-1985)

Jan 14 '08 #6
TomazK wrote:
[DllImport("libmysqld.dll")]
private static extern int mysql_server_init(int argc, string[] argv,
string[] groups);
{
string[] argv = new string[1];
argv[0] = "mysql_test";
string[] groups = new string[2];
groups[0] = "libmysqd_server";
groups[1] = "libmysqd_client";
mysql_server_init(0, argv, groups);
}
Assuming that argc is an argument count, it should probably not be 0,
and assuming that to each arg there is a group, you should probably
have argc=2, and also have two (2)strings in each array:

{
string[] argv = new string[2];
argv[0] = "mysql_test";
argv[1] = argv[0];

string[] groups = new string[2];
groups[0] = "libmysqd_server";
groups[1] = "libmysqd_client";

mysql_server_init(2, argv, groups);
}

Of course, I have no idea which strings are valid. Try to convert a C
or C++ example, if you have one, to C#.

--
Rudy Velthuis http://rvelthuis.de

"The object of war is not to die for your country but to make
the other bastard die for his."
-- General George Patton (1885-1945)
Jan 14 '08 #7
Yes, argc is a count and it doesn't matter if I specify 2 or 0 or any other
number. It's the same in all cases.
regards
Tomaz
"Rudy Velthuis" <ne********@rvelthuis.dewrote in message
news:xn****************@news.microsoft.com...
TomazK wrote:
>[DllImport("libmysqld.dll")]
private static extern int mysql_server_init(int argc, string[] argv,
string[] groups);
{
string[] argv = new string[1];
argv[0] = "mysql_test";
string[] groups = new string[2];
groups[0] = "libmysqd_server";
groups[1] = "libmysqd_client";
mysql_server_init(0, argv, groups);
}

Assuming that argc is an argument count, it should probably not be 0,
and assuming that to each arg there is a group, you should probably
have argc=2, and also have two (2)strings in each array:

{
string[] argv = new string[2];
argv[0] = "mysql_test";
argv[1] = argv[0];

string[] groups = new string[2];
groups[0] = "libmysqd_server";
groups[1] = "libmysqd_client";

mysql_server_init(2, argv, groups);
}

Of course, I have no idea which strings are valid. Try to convert a C
or C++ example, if you have one, to C#.

--
Rudy Velthuis http://rvelthuis.de

"The object of war is not to die for your country but to make
the other bastard die for his."
-- General George Patton (1885-1945)

Jan 15 '08 #8
On Jan 14, 10:10 am, "TomazK" <unkn...@abc.comwrote:
I tried with string[] but as soon as I call the function, whole application
terminates instantly with no error message. I haven't tried with
CharSet=CharSet.Ansi yet.

from MySQL specification the dll function looks like this: int
mysql_library_init(int argc, char **argv, char **groups)

my implementation:

[DllImport("libmysqld.dll")]
private static extern int mysql_server_init(int argc, string[] argv,
string[] groups);

{
string[] argv = new string[1];
argv[0] = "mysql_test";

string[] groups = new string[2];
groups[0] = "libmysqd_server";
groups[1] = "libmysqd_client";

mysql_server_init(0, argv, groups);

}
The function might expect groups and/or argv to be null-terminated.
Try adding an extra item to the end of each array and setting it to
null.

Jesse
Jan 17 '08 #9
Jesse McGrew wrote:
The function might expect groups and/or argv to be null-terminated.
Try adding an extra item to the end of each array and setting it to
null.
Could be. Good idea. But then I wonder what argc does there. <g>
--
Rudy Velthuis http://rvelthuis.de

"Wagner's music is better than it sounds."
-- Mark Twain (1835-1910)
Jan 17 '08 #10
On Jan 17, 10:36 am, "Rudy Velthuis" <newsgro...@rvelthuis.dewrote:
Jesse McGrew wrote:
The function might expect groups and/or argv to be null-terminated.
Try adding an extra item to the end of each array and setting it to
null.

Could be. Good idea. But then I wonder what argc does there. <g>
From the MySQL docs for this function at
http://dev.mysql.com/doc/refman/5.0/...rver-init.html it seems
there are two problems:

1. 'groups' is indeed supposed to be null-terminated. This is easy
enough to fix; just add a null at the end of the array.

2. Both 'argv' and 'groups' are stored by the library and used later.
This is a serious problem, because if you use P/Invoke marshaling, the
pointers that .NET passes to the function will become invalid as soon
as the function returns. When the library later tries to refer to argv
or groups, it'll find nothing but garbage there, causing unexpected
behavior or a crash.

If you're using MySQL 5.0.3 or later, try using mysql_library_init
instead - it makes an internal copy of argv and groups, so you can
keep using P/Invoke marshaling with no trouble (as long as you add a
null entry to the end of groups). Otherwise, you'll have to allocate
the memory yourself for the arrays and each string, to make sure the
pointers stay valid as long as your program is running.

Jesse
Jan 18 '08 #11
Jesse McGrew wrote:
From the MySQL docs for this function at
http://dev.mysql.com/doc/refman/5.0/...rver-init.html it seems
there are two problems:
What this shows is that the OP should probably have read the docs
first. <g>
--
Rudy Velthuis http://rvelthuis.de

"Only a free and unrestrained press can effectively expose
deception in government."
-- Hugo Black, Supreme Court Justice
Jan 18 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
12
by: Yuan Zhong | last post by:
Hi, Can someone please explain me what's going on during a call to a function. Specifically, I wanted to know what's going on in Stacks. Why is it ok to pass only 2 parameters or 5 parameters...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
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...
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...
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 ); ...
2
by: brian_harris | last post by:
I am tring to use trimstart to remove leading zeros, but all things I try give a compiler error on converting data. I am programing in C++ .net vs2003. This is one of my earlier attempts to call...
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...
8
by: keith | last post by:
Someone please help me out here - I'm having a 'bad brain' day. I have a number of C logging functions, prototyped as, say: extern "C" { mylog1(char *arg1, char *arg2, char *arg3);...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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...

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.