Re: Managed dll and native c dll
- From: Raj <Raj@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 1 Dec 2008 09:22:03 -0800
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:
- Follow-Ups:
- Re: Managed dll and native c dll
- From: Ben Voigt [C++ MVP]
- Re: Managed dll and native c dll
- Next by Date: Re: Managed dll and native c dll
- Next by thread: Re: Managed dll and native c dll
- Index(es):
Relevant Pages
|