Exposing method with const char *

M

Mike

I am just getting in .Net, how do you expose a C++/CLR class function
so that in VB.NET its prototype is:

function XYZ(byval p as integer, byval s as string) as boolean

I have this, but the compiler doesn't like it:

namespace MyAPICLR {
public ref class MyClass
{
...
static bool XYZ(int p, const String^ s)
{
return ::XYZ(HWND)p,s); << How do typecast this?
}
};
}

The C++ class XYZ function wraps a imported lib function ::XYZ whose
WIN32 C/C++ prototype is:

BOOL APIENTRY XYZ(HWND p, const char *s);

I'm sorry, I know its s damn simple. I can't find an example. :-(

Why can't the compiler see:

static bool XYZ(int p, const char *s)

and know that the reference exposure is a const non-byref System::String?

--
 
M

Mike

Got it;

static bool XYZ(int p, String^ server)
{
char* psz = (char*)(void*)Marshal::StringToHGlobalAnsi(s);
return ::XYZ((HWND)parent,psz);
}

Is that the right solution? It does work. I was hoping to use C++ to
create my class library to minimize unmanaged marshalling when done
via a VB.NET or C# equivalent class library.

Thanks
 
D

David Wilkinson

Mike said:
Got it;

static bool XYZ(int p, String^ server)
{
char* psz = (char*)(void*)Marshal::StringToHGlobalAnsi(s);
return ::XYZ((HWND)parent,psz);
}

Is that the right solution? It does work. I was hoping to use C++ to
create my class library to minimize unmanaged marshalling when done via
a VB.NET or C# equivalent class library.

Mike:L

I'm not a big expert at managed C++, but I think you should be using IntPtr.
It's also best not to use C-style casts in C++. And don't convert to char* if
all you need is const char*.

// untested
static bool XYZ(IntPtr p, String^ s)
{
HWND hWnd = static_cast<HWND>(p.ToPointer());
IntPtr q = Marshal::StringToHGlobalAnsi(s);
const char* psz = static_cast<const char*>(q.ToPointer());
return ::XYZ(hWnd, psz);
}
 
M

Mike

Thanks David,

I followed the KB examples at:

http://support.microsoft.com/kb/311259

My current final version is:

static bool XYZ(int parent, String^ server)
{
const char* psz = NULL;
__try {
psz = (const har*)(void*)Marshal::StringToHGlobalAnsi(server);
return ::XYZ((HWND)parent,psz);
} __finally {
if (psz) {
Marshal::FreeHGlobal(IntPtr((void*)psz));
}
}
}

I'll fix up the HWND parameter per your note. But geez, .NET is
fascinated but so darn bloated and need to learn even more "wrappers!"
but thats a just a side note.

I am wondering if I should use [DLLImport] for this and the 150+ other
win32 DLL functions I am importing. I guess I can also just make
my win32 header file more c++/clr aware. Is this the new C++? It is
called C++++++? <g>

Thanks
 
D

David Wilkinson

Mike said:
Thanks David,

I followed the KB examples at:

http://support.microsoft.com/kb/311259

My current final version is:

static bool XYZ(int parent, String^ server)
{
const char* psz = NULL;
__try {
psz = (const har*)(void*)Marshal::StringToHGlobalAnsi(server);
return ::XYZ((HWND)parent,psz);
} __finally {
if (psz) {
Marshal::FreeHGlobal(IntPtr((void*)psz));
}
}
}

Mike:

Ah yes, you need to use FreeHGlobal(). I forgot about that.
 

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