what is wrong with this

G

gs

I want to pass a string array to C which uses ansi

in a test, I tried:
unsafe public int ctstAny1dp(String*[] strOut)
{
private string[] strMyC = {
"abcdef string 1",
"abcdef string 2",
"abcdef string 3",
"abcdef string 4",
"abcdef string 5"},
str2 = { "str2_1", "str2 2" };
// the above will be replaced by some other processing in production

// can't implicitly convert: strOut = strMyC;
for (int i = 0; i < strMyC.GetUpperBound(0); i++)
{
strOut =
((Marshal.StringToHGlobalAnsi(strMyC)).ToPointer()); };
return strMyC.GetUpperBound(0) + 1;
}

the above won't compile: Error 1 Cannot implicitly convert type 'void*' to
'string*'. An explicit conversion exists (are you missing a cast?)
D:\data\Ieproj\dotnet\IeStringClass\C#\ieStringCS\ieStringCS\ieString.cs 99
26 ieStringCS


I thought I am getting a pointer to the string.

Is there a better way
 
G

Guest

strOut =
((Marshal.StringToHGlobalAnsi(strMyC)).ToPointer()); };


This:

((Marshal.StringToHGlobalAnsi(strMyC)).ToPointer());

returns a void* , you need to cast it to String*
 
G

gs

Thx
but if I do this
strOut = (String*)
((Marshal.StringToHGlobalAnsi(strMyC)).ToPointer());

I get
Error 1 Cannot take the address of, get the size of, or declare a pointer to
a managed type ('string')
D:\data\Ieproj\dotnet\IeStringClass\C#\ieStringCS\ieStringCS\ieString.cs 100
26 ieStringCS

PIEBALD said:
strOut =
((Marshal.StringToHGlobalAnsi(strMyC)).ToPointer()); };


This:

((Marshal.StringToHGlobalAnsi(strMyC)).ToPointer());

returns a void* , you need to cast it to String*
 
G

Guest

Then you'll need someone better at it than I. Chances are you shouldn't be
producing an array of string pointers.

What is it you're trying to accomplish anyway?
 
G

gs

I am just trying to find a way to pass via com interface to native
application caller a string array.

The desired native calling protocol is
// setup and connect muyoleobj to the c# com class..
// ...
string [] mystrArray[]
myoleobj.ctstAny1dp(mystrArray)
 
G

Guest

Well I don't do COM, but I expect what you want is one method that handles
both the converting and the calling.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top