CLI/C++: unmanaged structure to managed code in WinProc

G

Guest

Hi,

I have CLI/C++ code to override WndProc(Message% m) in order to create NC
area. I tried to marshal unmanaged ptr (m.LParam) to managed object by using
Marshal::ptrToStructure like:
if (m.WParam == IntPtr(1))
{
NCCALCSIZE_PARAMS csp;

csp = (NCCALCSIZE_PARAMS)Marshal::ptrToStructure(m.LParam,
(NCCALCSIZE_PARAMS));
csp.rgrc[0].top += borderWidth;
csp.rgrc[0].bottom -= borderWidth;
csp.rgrc[0].left += borderWidth;
csp.rgrc[0].right -= borderWidth;
Marshal::StructureToPtr(csp, m.LParam, false);
}
I'm having difficult in creating the managed type of the NCCALCSIZE_PARAMS
in order to pass to PtrToStructure as the second parameter. I've tried some
suggestions from the forum unsuccessfully. In C#, it's easily done by
typeof(NCCALCSIZE_PARAMS). Is any easy way to translate this C# yntax to
CLI/C++ equavilent. Or, do I have to do in a hard way -- create a managed
equavilent value structure, copy native pointered NCCALCSIZE_PARAMS data into
the managed structure and then marshal back to native structure ptr after
modifying data in order to pass out WinProc. It's messy. Why doesn't CLI/C++
have an easy syntax as C# in this regard. Anyone has a good idea of it?

Thanks
 
G

Guest

After examing IntPtr, there is no need to use PtrToStructure in CLI/C++. So,
the code should be simply as
NCCALCSIZE_PARAMS* csp = (NCCALCSIZE_PARAMS*)m.LParam.ToPointer();
 
B

Ben Voigt [C++ MVP]

cjs said:
After examing IntPtr, there is no need to use PtrToStructure in CLI/C++.
So,
the code should be simply as
NCCALCSIZE_PARAMS* csp = (NCCALCSIZE_PARAMS*)m.LParam.ToPointer();

Exactly. You've hit on one of the biggest advantages of C++/CLI -- it
understands native types.

If you ever did need the equivalent of C# typeof(MyClass), it's
MyClass::typeid
cjs said:
Hi,

I have CLI/C++ code to override WndProc(Message% m) in order to create NC
area. I tried to marshal unmanaged ptr (m.LParam) to managed object by
using
Marshal::ptrToStructure like:
if (m.WParam == IntPtr(1))
{
NCCALCSIZE_PARAMS csp;

csp = (NCCALCSIZE_PARAMS)Marshal::ptrToStructure(m.LParam,
(NCCALCSIZE_PARAMS));
csp.rgrc[0].top += borderWidth;
csp.rgrc[0].bottom -= borderWidth;
csp.rgrc[0].left += borderWidth;
csp.rgrc[0].right -= borderWidth;
Marshal::StructureToPtr(csp, m.LParam, false);
}
I'm having difficult in creating the managed type of the
NCCALCSIZE_PARAMS
in order to pass to PtrToStructure as the second parameter. I've tried
some
suggestions from the forum unsuccessfully. In C#, it's easily done by
typeof(NCCALCSIZE_PARAMS). Is any easy way to translate this C# yntax to
CLI/C++ equavilent. Or, do I have to do in a hard way -- create a managed
equavilent value structure, copy native pointered NCCALCSIZE_PARAMS data
into
the managed structure and then marshal back to native structure ptr after
modifying data in order to pass out WinProc. It's messy. Why doesn't
CLI/C++
have an easy syntax as C# in this regard. Anyone has a good idea of it?

Thanks
 

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