Asim Patnaik

Hi,

I have a Win32 dll which returns a vector of strings. I call it from a C# client method using P/Invoke. But what I get is only IntPtr.

How to convert this IntPtr to an arraylist

Is there any type in C# that directly maps to vector so that I can directly get an object of that type

Regards,

Asim.



Re: Visual C# Express Edition Returning vector from Win32 dll to C#

rauhanlinnake

Vector of strings could map to one dimensional string array




Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Asim Patnaik

Hi,

What I intend to ask is where and how to convert the vector of strings from the Win32 dll

On the C# client, I get an IntrPtr. How do I convert this into an array of strings (or Arraylist).

Or do I need to pass any object which gets filled up in the Win32 dll

Please can anybody help me with a sample code

Regards,

Asim.





Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Citizen on the earth

Hi Asim,

Based on my understanding, you would like to convert the vector of the strings from Win32 to C# client.

You can try the following sample codes and check if it works for you.

string vecString = Marshal.PtrToStringAnsi(IntPtr); or string vecString = Marshal.PtrToStringBSTR(IntPtr);

ArrayList ar = new ArrayList();
ar.Add(vecString);

Hope this helps,

Regards,

Citizens on the earth





Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Asim Patnaik

Hi,

The C++ code is something like this:

Code Snippet

vector GetVector()

{

int i;

i = 10;

vector strVector;

strVector.reserve(i);

for(int j=0;j<I;J++)< P>

{

strVector.push_back("test");

}

return strVector;

}

The C# client call is given below:

Code Snippet

IntPtr ptr = GetVector();

The C++ code is returning a vector of strings. So the client needs to receive it as an arraylist of strings or array of strings or something similar.
So my doubt is how to achieve this.
Can anybody please help me with this with an example
Regards,
Asim.




Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Citizen on the earth

Hi Asim,

As far as I know, there is no direct support to Vector before .NET 3.0, which equals to ArrayList before .NET 2.0 or Generic List in .NET 2.0. If you would like to interop a C++ Vector in C#, you'd better write a wrapper in C++ that exposes the data like a managed array or List instead. In the next version of .NET Framework(.NET 3.0), it provides supports to Vector. If you are interested in it, try to check out this document for details - http://msdn2.microsoft.com/en-us/library/system.windows.vector.aspx

There is a discussion thread about "interop between C++ std::vector and C# application" - http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2102882&SiteID=1, and you can check out it.

Hope this helps,

Regards,

Citizens on the earth





Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Asim Patnaik

Hi,

Thanks for the reply. Currently we are using .Net 2.0 and we have to stick to it for now atleast. So I am looking for a solution in .Net 2.0.

So you mean to say that I need to build a managed C++ wrapper around the current Win32 dll. And this C++ wrapper will covert the Win32 vector to an Arraylist (or something similar) and send it back to the C# client Is vector supported in managed C++

Regards,

Asim.





Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Citizen on the earth

Hi Asim,

Thanks for your quick update.

As far as I know, Vector isn't supported in managed C++ before .NET 3.0. If you would like to write a C++ wrapper to convert the return value Vector to an Array, maybe the unmanaged C++ is your choice. Then you can call it in C# client.

Hope this helps,

Regards,

Citizens on the earth





Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Mattias Sjogren

Guys I think you're talking about different vector classes here. The .NET 3.0 System.Windows.Vector class is very different from the C++ std::vector.






Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Asim Patnaik

Hi,

Thanks for the reply. Let me ask you this from the implementation point of view.

1. I need to create an unmanaged C++ wrapper around the existing Win32 dll.

2. There has to be a structure implemented on both the C# client and the wrapper.

3. The wrapper calls the Win32 method on behalf of the C# client.

4. The wapper converts the vector of strings to this common structure and returns an IntPtr to the C# client.

5. The C# client converts this IntPtr to the structure to get the data.

Is this what I need to do to get the data Can anybody help me with a sample code

I tried returning a string and an int as a part of the structure from the wrapper and I received the int value but received string as null on the C# client. Please explain this to me as well.

Regards,

Asim.





Re: Visual C# Express Edition Returning vector from Win32 dll to C#

Woooo121

A simple method is: Return a xml string in your wrapper class. So the interface between C# and C++ only need to handle a string return. It's very easy. Simple but maybe not efficient. You can try. C# client can use some XML--Object mapping even Deserial from xml to get some object contains all the data.