C# - C Interop

  • Thread starter Thread starter Germic
  • Start date Start date
G

Germic

Hi,

I need to pass a reference to a string between a C and C# library.

I declare the string in C# function as ref String xyz and the corresponding
C code would refer to it as BSTR *xyzz. However i get an error as i feel the
string is not marshalled properly.

What is the correct way to do this?

Thanks
 
Germic said:
Hi,

I need to pass a reference to a string between a C and C# library.

I declare the string in C# function as ref String xyz and the
corresponding
C code would refer to it as BSTR *xyzz. However i get an error as i feel
the
string is not marshalled properly.

What is the correct way to do this?

Thanks
If the function is not a COM interface method you have to tag the argument
with a MarshalAs attribute:

[MarshalAs(UnmanagedType.BStr)]ref string s

Willy.
 
I've been trying to find a solution for quite sometime. Hope this time
someone could help.

I query a webservice from a C DLL through a C# Library. The webservice
returns an XML file which contains an array of array. Now, my C DLL can
consume only 1 array at a time. So the first hit from the C DLL to the C#
Library gets the whole bunch of data from the webservice and gives the C DLL
just the first array of data. On the next call from the C DLL to the C#
library, the C# library has to give the next array of data from the XML
string it has previously received from the webservice.

I've tried parsing the XML and putting it in a hashtable, but i am not able
to pass the address of the hashtable between the C and C# functions as a
parameter (&, ref). How can I create a reference to hashtable in C???

I'm trying to pass the whole XML string back and forth. Object at C# end and
VARIANT at C end. Then i parse the XML string into a dataset and put it into
a foreach loop everytime, get the hit-number from the C DLL and run a
counter within the foreach loop till it matches and then put the data in the
array onto a structure to be returned to the C DLL.

Is there any better way to do it?
 
never mind.. tried using VARIANT at the C end and OBJECT at the C# end,
seems to work fine!

Thanks anyway. Any other suggestions is welcomed.
 
Back
Top