Re: Managed dll and native c dll

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Can I call this code in a function

ConvertDesc(pin_ptr<wchar_t> **managedPtr)
{
pin_ptr<wchar_t> *managedPtr = PtrToStringChars(myManagedDesc.str1);
WideCharToMultiByte(CP_ACP, 0, *managedPtr, myManagedDesc.str1->Length,
myNativeDesc.str1, sizeof myNativeDesc.str1, NULL, NULL);
}

I want to pass a pointer to pin pointer to the function. Will the
pin_pointer still be effective if I use pointer-to-pointer syntax ?

Thanks


"Ben Voigt [C++ MVP]" wrote:

Raj wrote:
I have a struct like this:

struct ManagedDesc1
{
String str1;
String str2;
};

struct NativeDesc1
{
char str1[20];
char str2[20];
};

I receive struct ManagedDesc1 from C# App to .Net C++ dll. I have to
now pass to native dll struct Nativedesc1. Can I just pass it? It
wont work I guess. I have to use marshall class and convert str1 and
str2 String to std:string after declaring local Native struct in
wrapper.

This is about the worst case scenario, because you need a Unicode -> ANSI
conversion.

You can do that with no extra copies by doing this:

pin_ptr<wchar_t> managed = PtrToStringChars(myManagedDesc.str1);
WideCharToMultiByte(CP_ACP, 0, managed, myManagedDesc.str1->Length,
myNativeDesc.str1, sizeof myNativeDesc.str1, NULL, NULL);


Thanks,
Raj


"Raj" <Raj@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:EBAF6682-8A30-4D7F-8740-23AE08827978@xxxxxxxxxxxxxxxx
Thanks Ben.

I have lot of confusion with IJW. I dont know what works and what
doesnot work. P/Invoke is lot clear - you have to do work to make
it work. My I chose
this C++ interop because I am working on a performance sensitive
project.

For example, you said C++ has its own marshall class. If it just
works, why
do I need this marshall class to convert System::String to
std::string. May be this is stupid question. But then I didnot find
much details on C++ interop. All details on P/Invoke explicit only.

Calling both types of code just works. But to call a function you
need to supply its parameters and they all have to be the right
type. So there might still be times you need to convert. For
example, if your native code uses std::string or CString, but you
were sent a string from C# which is a System::String, you'd not be
able to call the native code using the System::String.

This is just the same as trying to use STL algorithms with an MFC
CArrayList. It's not possible without conversion.


Thanks,
Raj

"Ben Voigt [C++ MVP]" wrote:



.



Relevant Pages

  • Re: how to store list of varying types
    ... When there's a variable-length string, ... typedef struct { ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ...
    (microsoft.public.vc.mfc)
  • Re: Managed dll and native c dll
    ... I want to pass a pointer to pin pointer to the function. ... struct ManagedDesc1 ... I receive struct ManagedDesc1 from C# App to .Net C++ dll. ... now pass to native dll struct Nativedesc1. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Managed dll and native c dll
    ... I have a struct like this: ... String str1; ... char str1; ... pass to native dll struct Nativedesc1. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Managed dll and native c dll
    ... struct ManagedDesc1 ... String str1; ... I receive struct ManagedDesc1 from C# App to .Net C++ dll. ... now pass to native dll struct Nativedesc1. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Help with C->C# Port
    ... char is a character. ... you would use a string in C# ... - **char is a pointer to a pointer of char. ... There are struct in C# too. ...
    (microsoft.public.dotnet.languages.csharp)