C# SendMessage to C++ WinProc - c#

I need to send a string from C# to a C++ WindowProc. There are a number of related questions on SO related to this, but none of the answers have worked for me. Here's the situation:
PInvoke:
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, string lParam);
C#:
string lparam = "abc";
NativeMethods.User32.SendMessage(handle, ConnectMsg, IntPtr.Zero, lparam);
C++:
API LRESULT CALLBACK HookProc (int code, WPARAM wParam, LPARAM lParam)
{
if (code >= 0)
{
CWPSTRUCT* cwp = (CWPSTRUCT*)lParam;
...
(LPWSTR)cwp->lParam <-- BadPtr
...
}
return ::CallNextHookEx(0, code, wParam, lParam);
}
I've tried a number of different things, Marshalling the string as LPStr, LPWStr, also tried creating an IntPtr from unmanaged memory, and writing to it with Marshal.WriteByte.
The pointer is the correct memory location on the C++ side, but the data isn't there. What am I missing?

For C++ LPWSTR or LPSTR parameters you need to use the C# StringBuilder in your DllImport.
For C++ LPCWSTR or LPCSTR parameters you need to use the C# string in your DllImport.

Make sure that your SendMessage call is occurring in the expected, synchronous manner and that your NativeMethods class maps the proper Win32 call (Send vs. PostMessage.) If this isn't correct it's possible that by the time your message is processed on the C++ end, you've left the scope of your C# method and any local variables created on the stack are gone resulting in your bad pointer.
Stack and heap considerations for cross-thread calls: Threads have their own stacks but share the heap. Stack-allocated variables in one thread will not be visible in another. The string type is an odd duck in .NET. It is an Object-derived, reference type but made to look and feel like a value type in code. So perhaps passing a pointer to heap-allocated data ought to work. That's where StringBuilder, as a heap-allocated reference type, comes in.

Related

P/Invoke vkGetPhysicalDeviceFeatures Access Violation

I'm working on a Vulkan wrapper for C# (like I'm sure many people are) and I'm having a bit of a problem with vkGetPhysicalDeviceFeatures it either doesn't return data, or throws Access Violations
Unanaged Side - Signature:
The signature from the spec is this:
void vkGetPhysicalDeviceFeatures(
VkPhysicalDevice physicalDevice,
VkPhysicalDeviceFeatures* pFeatures);
VkPhysicalDevice is a handle object defined as:
#define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
VK_DEFINE_HANDLE(VkPhysicalDevice)
This is just a pointer and other imports using IntPtr or SafeHandle wrappers for objects of this shape work.
Managed Side - DLL Import:
Expected DLL Import (but failing):
[DllImport("vulkan-1.dll", EntryPoint = "vkGetPhysicalDeviceFeatures")]
internal static extern void GetPhysicalDeviceFeatures(PhysicalDeviceHandle physicalDevice, ref IntPtr features);
This is similar to other working imports. Note: PhysicalDeviceHandle is derived from SafeHandle should be marshalled to IntPtr, I have other imports with this pattern that work. The above throws an access violation when called.
Platform:
Windows 10 (x64)
Nvidia Driver: 356.43-vkonly (latest)
Update
#V. Kravchenko was correct
There was nothing wrong with the import above. My issue was actually with the vkEnumeratePhysicalDevices call.
First off I had the import wrong, the correct import looks like:
[DllImport("vulkan-1.dll", EntryPoint = "vkEnumeratePhysicalDevices ")]
internal static extern Result EnumeratePhysicalDevices (InstanceHandle instance, ref physicalDeviceCount, IntPtr[] physicalDevices);
Second off, I was actually using the function incorrectly. You need to call vkEnumeratePhysicalDevices twice. The first call gets the count of devices, the second call populates the array of devices.:
IntPtr[] devices = new IntPtr[]();
uint deviceCount = 0;
// populates deviceCount with the number of devices
Vk.EnumeratePhysicalDevices(instanceHandle, ref deviceCount, null);
// populates the devices array with the handle to each device, will only populate up to deviceCount devices
Vk.EnumeratePhysicalDevices(instanceHandle, ref deviceCount, devices);
Note: this is outlined in the description/valid usage section of the function's documentation, I just didn't interpret it correctly on the first read through.
Once I finally had the right handle values from EnumeratePhysicalDevices then my final call to GetPhysicalDeviceFeatures worked as expected. The final import for GetPhysicalDeviceFeatures looks like:
[DllImport("vulkan-1.dll", EntryPoint = "vkGetPhysicalDeviceFeatures")]
internal static extern void GetPhysicalDeviceFeatures(PhysicalDeviceHandle physicalDevice, ref VkPhysicalDeviceFeatures features);
Note: any variable with Handle in the name is a subclass of SafeHandle.
You must IntPtr, which actually does point to a valid object, but not just IntPtr. Access violation means that code in dll is trying to access memory, that your IntPtr points and can't, because your IntPtr doesn't point to a valid object.
Overall, you should use your expected variant, but pass pointer to a valid object.
Working variant is working, because ref IntPtr is actually a pointer to IntPtr, and IntPtr object's location, to which ref IntPtr is pointing, is valid memory.

Mangled name even after extern c

I have included a C++ library in my C# project and i am calling one of it's method.
Earlier I was having the mangling problem then read about extern c and applied it to C++ method.
Then tried calling it like below:
[DllImport(#"F:\bin\APIClient.dll")]
public static extern IntPtr logIn2(IntPtr a, IntPtr b, IntPtr c, IntPtr d, IntPtr e, IntPtr f, int g);
But still I am getting Entry Point exception.
C++:
APICLIENT_API char* logIn2(const char* a, const char* b,const char* c,const char* d,const char* e,const char* f, int g);
And if i use entryPoint in DLLImport then it works fine:
[DllImport(#"F:\bin\APIClient.dll", EntryPoint = "?logIn2#CAPIClient#API##QAEPADPBD00000H#Z", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr logIn2(IntPtr a, IntPtr b, IntPtr c, IntPtr d, IntPtr e, IntPtr f, int g);
Why even after using extern c I have to give this Entry point to make things working.
The decorated C++ name is not a problem. It is actually very desirable, it automatically saves you from having to diagnose a very difficult runtime crash when the C++ code is changed and the function signature is altered. Since there now will be a mismatch and you get an easy "Procedure not found" error message instead of a corrupted call stack that is quite undiagnosable.
The much bigger problem, and the reason that extern "C" doesn't work, is that this is an instance method of the CAPIClient class. It uses the __thiscall calling convention, required to pass a valid this pointer. You can't get that from pinvoke, it requires allocating memory for the C++ object and calling the CAPIClient constructor. Only a C++ compiler knows how to do that correctly, only it knows the correct amount of memory to allocate. So you can't pinvoke, you have to write a C++/CLI wrapper.
The normal mishap when you pinvoke an instance method of a C++ class is a hard crash, typically reported as an AccessViolationException. Triggered when the instance method tries to access another other instance member of the C++ class through the invalid this pointer. Only a static C++ function can be correctly pinvoked. Since you didn't seem to have triggered an exception (yet), there's some hint that the function should have been static in the first place.

DataType between different process and architectures - Writing a correct DllImport

I have a main application written in C# that runs as a x64 bit application, it communicates through dll import with a standard native unmanaged C/C++ dll of which I have also the header.
I need help for setting out the correct dataTypes.
So i expose one of the methods I have to call and the data types defined in the dll header.
typedef int DLL_IMP_EXP (* INJECTDLL)(HANDLE, DWORD, LPCTSTR, DWORD, HINSTANCE *);
HANDLE is defined as void*
DWORD is defined as unsigned long
LPCTSTR is defined as __nullterminated CONST CHAR*
HINSTANCE gives me this line for definition: DECLARE_HANDLE(HINSTANCE); ?!?
Using Unicode declaration of the function:
LPCWSTR is defined as __nullterminated CONST WCHAR*
Please help me writing the correct declaration of the:
[DllImport ("Inject.dll", VariousParameters)]
public static extern int InjectDll(CorrectDataTypes);
Compiling VariousParameters if needed, and obviously CorrectDataTypes.
And IntPtr is used for a pointer or a handle - it will be 32 bits on a 32 bit system and 64 on a 64 bit system. So if you have anything that is a raw pointer or a handle use an IntPtr and it will work correctly on both systems. However your last parameter is a pointer to a handle - use a ref to handle the pointer. So in this case since it's a pointer to a handle, the parameter will be a ref to an IntPtr.
For standard numeric types, those will map directly to the .NET data types - you can get more details at MSDN.
Null terminated strings are handled correctly, although you'll need to specify whether it uses ANSI or Unicode strings.
Finally P/Invoke assumes a StdCall calling convention (which is what the Windows API uses). If you're not using that, which the function prototype would include STDCALL or __stdcall in it, the standard C calling convention is Cdecl. Although you'll have to find out what DLL_IMP_EXP expands to.
So your P/Invoke declaration should be:
[DllImport ("Inject.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern int InjectDll(IntPtr handle, uint dword, string str, uint dword2, ref IntPtr hInstance);

Passing StringBuilder to PInvoke function

In one of the post Titled "Call a c++ method that returns a string, from c#"
Its said that , to make the following Pinvoke to work change the C++ signature to as
extern "C" REGISTRATION_API void calculate(LPSTR msg)
C++ code
extern "C" REGISTRATION_API void calculate(char* msg)
C# code
[DllImport("thecpp.dll", CharSet=CharSet.Ansi)]
static extern void calculate(StringBuilder sMsg);
How can stringBuilder which is a class ,convertd to long ptr to string .(but this is the accepted answer)
Shouldnt we use use IntPtr as below ?
extern "C" REGISTRATION_API void calculate(Intptr msg)
Look for the section marked "Passing Strings", the marshaler has got some added smarts to do this trick.
http://msdn.microsoft.com/en-us/library/aa446536.aspx
To solve this problem (since many of
the Win32 APIs expect string buffers)
in the full .NET Framework, you can,
instead, pass a
System.Text.StringBuilder object; a
pointer will be passed by the
marshaler into the unmanaged function
that can be manipulated. The only
caveat is that the StringBuilder must
be allocated enough space for the
return value, or the text will
overflow, causing an exception to be
thrown by P/Invoke.

.NET Interop IntPtr vs. ref

Probably a noob question but interop isn't one of my strong points yet.
Aside from limiting the number of overloads is there any reason I should declare my DllImports like:
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
And use them like this:
IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(formatrange));
Marshal.StructureToPtr(formatrange, lParam, false);
int returnValue = User32.SendMessage(_RichTextBox.Handle, ApiConstants.EM_FORMATRANGE, wParam, lParam);
Marshal.FreeCoTaskMem(lParam);
Rather than creating a targeted overload:
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref FORMATRANGE lParam);
And using it like:
FORMATRANGE lParam = new FORMATRANGE();
int returnValue = User32.SendMessage(_RichTextBox.Handle, ApiConstants.EM_FORMATRANGE, wParam, ref lParam);
The by ref overload ends up being easier to use but I'm wondering if there is a drawback that I'm not aware of.
Edit:
Lots of great info so far guys.
#P Daddy: Do you have an example of basing the struct class off an abstract (or any) class? I changed my signature to:
[DllImport("user32.dll", SetLastError = true)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] CHARFORMAT2 lParam);
Without the In, Out, and MarshalAs the SendMessage (EM_GETCHARFORMAT in my test) fail. The above example works well but if I change it to:
[DllImport("user32.dll", SetLastError = true)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] NativeStruct lParam);
I get a System.TypeLoadException that says the CHARFORMAT2 format is not valid (I'll try and capture it for here).
The exception:
Could not load type 'CC.Utilities.WindowsApi.CHARFORMAT2' from assembly 'CC.Utilities, Version=1.0.9.1212, Culture=neutral, PublicKeyToken=111aac7a42f7965e' because the format is invalid.
The NativeStruct class:
public class NativeStruct
{
}
I've tried abstract, adding the StructLayout attribute, etc. and I get the same exception.
[StructLayout(LayoutKind.Sequential)]
public class CHARFORMAT2: NativeStruct
{
...
}
Edit:
I didn't follow the FAQ and I asked a question that can be discussed but not positively answered. Aside from that there has been lot's of insightful information in this thread. So I'll leave it up to the readers to vote up an answer. First one to over 10 up-votes will be the answer. If no answer meets this in two days (12/17 PST) I'll add my own answer that summarizes all the yummy knowledge in the thread :-)
Edit Again:
I lied, accepting P Daddy's answer because he is the man and has been a great help (he has a cute little monkey too :-P)
If the struct is marshalable without custom processing, I greatly prefer the latter approach, where you declare the p/invoke function as taking a ref (pointer to) your type. Alternatively, you can declare your types as classes instead of structs, and then you can pass null, as well.
[StructLayout(LayoutKind.Sequential)]
struct NativeType{
...
}
[DllImport("...")]
static extern bool NativeFunction(ref NativeType foo);
// can't pass null to NativeFunction
// unless you also include an overload that takes IntPtr
[DllImport("...")]
static extern bool NativeFunction(IntPtr foo);
// but declaring NativeType as a class works, too
[StructLayout(LayoutKind.Sequential)]
class NativeType2{
...
}
[DllImport("...")]
static extern bool NativeFunction(NativeType2 foo);
// and now you can pass null
<pedantry>
By the way, in your example passing a pointer as an IntPtr, you've used the wrong Alloc. SendMessage is not a COM function, so you shouldn't be using the COM allocator. Use Marshal.AllocHGlobal and Marshal.FreeHGlobal. They're poorly named; the names only make sense if you've done Windows API programming, and maybe not even then. AllocHGlobal calls GlobalAlloc in kernel32.dll, which returns an HGLOBAL. This used to be different from an HLOCAL, returned by LocalAlloc back in the 16-bit days, but in 32-bit Windows they are the same.
The use of the term HGLOBAL to refer to a block of (native) user-space memory just kind of stuck, I guess, and the people designing the Marshal class must not have taken the time to think about how unintuitive that would be for most .NET developers. On the other hand, most .NET developers don't need to allocate unmanaged memory, so....
</pedantry>
Edit
You mention you're getting a TypeLoadException when using a class instead of a struct, and ask for a sample. I did up a quick test using CHARFORMAT2, since it looks like that's what you're trying to use.
First the ABC1:
[StructLayout(LayoutKind.Sequential)]
abstract class NativeStruct{} // simple enough
The StructLayout attribute is required, or you will get a TypeLoadException.
Now the CHARFORMAT2 class:
[StructLayout(LayoutKind.Sequential, Pack=4, CharSet=CharSet.Auto)]
class CHARFORMAT2 : NativeStruct{
public DWORD cbSize = (DWORD)Marshal.SizeOf(typeof(CHARFORMAT2));
public CFM dwMask;
public CFE dwEffects;
public int yHeight;
public int yOffset;
public COLORREF crTextColor;
public byte bCharSet;
public byte bPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string szFaceName;
public WORD wWeight;
public short sSpacing;
public COLORREF crBackColor;
public LCID lcid;
public DWORD dwReserved;
public short sStyle;
public WORD wKerning;
public byte bUnderlineType;
public byte bAnimation;
public byte bRevAuthor;
public byte bReserved1;
}
I've used using statements to alias System.UInt32 as DWORD, LCID, and COLORREF, and alias System.UInt16 as WORD. I try to keep my P/Invoke definitions as true to SDK spec as I can. CFM and CFE are enums that contain the flag values for these fields. I've left their definitions out for brevity, but can add them in if needed.
I've declared SendMessage as:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(
HWND hWnd, MSG msg, WPARAM wParam, [In, Out] NativeStruct lParam);
HWND is an alias for System.IntPtr, MSG is System.UInt32, and WPARAM is System.UIntPtr.
[In, Out] attribute on lParam is required for this to work, otherwise, it doesn't seem to get marshaled both directions (before and after call to native code).
I call it with:
CHARFORMAT2 cf = new CHARFORMAT2();
SendMessage(rtfControl.Handle, (MSG)EM.GETCHARFORMAT, (WPARAM)SCF.DEFAULT, cf);
EM and SCF are enums I've, again, left out for (relative) brevity.
I check success with:
Console.WriteLine(cf.szFaceName);
and I get:
Microsoft Sans Serif
Works like a charm!
Um, or not, depending on how much sleep you've had and how many things you're trying to do at once, I suppose.
This would work if CHARFORMAT2 were a blittable type. (A blittable type is a type that has the same representation in managed memory as in unmanaged memory.) For instance, the MINMAXINFO type does work as described.
[StructLayout(LayoutKind.Sequential)]
class MINMAXINFO : NativeStruct{
public Point ptReserved;
public Point ptMaxSize;
public Point ptMaxPosition;
public Point ptMinTrackSize;
public Point ptMaxTrackSize;
}
This is because blittable types are not really marshaled. They're just pinned in memory—this keeps the GC from moving them—and the address of their location in managed memory is passed to the native function.
Non-blittable types have to be marshaled. The CLR allocates unmanaged memory and copies the data between the managed object and its unmanaged representation, making the necessary conversions between formats as it goes.
The CHARFORMAT2 structure is non-blittable because of the string member. The CLR can't just pass a pointer to a .NET string object where a fixed-length character array is expected to be. So the CHARFORMAT2 structure must be marshaled.
As it would appear, for correct marshaling to occur, the interop function must be declared with the type to be marshaled. In other words, given the above definition, the CLR must be making some sort of determination based on the static type of NativeStruct. I would guess that it's correctly detecting that the object needs to be marshaled, but then only "marshaling" a zero-byte object, the size of NativeStruct itself.
So in order to get your code working for CHARFORMAT2 (and any other non-blittable types you might use), you'll have to go back to declaring SendMessage as taking a CHARFORMAT2 object. Sorry I led you astray on this one.
Captcha for the previous edit:
the whippet
Yeah, whip it good!
Cory,
This is off topic, but I notice a potential problem for you in the app it looks like you're making.
The rich textbox control uses standard GDI text-measuring and text-drawing functions. Why is this a problem? Because, despite claims that a TrueType font looks the same on screen as on paper, GDI does not accurately place characters. The problem is rounding.
GDI uses all-integer routines to measure text and place characters. The width of each character (and height of each line, for that matter) is rounded to the nearest whole number of pixels, with no error correction.
The error can easily be seen in your test app. Set the font to Courier New at 12 points. This fixed-width font should space characters exactly 10 per inch, or 0.1 inches per character. This should mean that, given your starting line width of 5.5 inches, you should be able to fit 55 characters on the first line before wrap occurs.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123
But if you try, you'll see that wrap occurs after only 54 characters. What's more the 54th character and part of the 53rd overhang the apparent margin shown on the ruler bar.
This assumes you have your settings at standard 96 DPI (normal fonts). If you use 120 DPI (large fonts), you won't see this problem, although it appears that you size your control incorrectly in this case. You also won't likely see this on the printed page.
What's going on here? The problem is that 0.1 inches (the width of one character) is 9.6 pixels (again, using 96 DPI). GDI doesn't space characters using floating point numbers, so it rounds this up to 10 pixels. So 55 characters takes up 55 * 10 = 550 pixels / 96 DPI = 5.7291666... inches, whereas what we were expecting was 5.5 inches.
While this will probably be less noticeable in the normal use case for a word processor program, there is a likelihood of instances where word wrap occurs at different places on screen versus on page, or that things don't line up the same once printed as they did on screen. This could turn out to be a problem for you if this is a commercial application you're working on.
Unfortunately, the fix for this problem is not easy. It means you'll have to dispense with the rich textbox control, which means a huge hassle of implementing yourself everything it does for you, which is quite a lot. It also means that the text drawing code you'll have to implement becomes fairly complicated. I've got code that does it, but it's too complex to post here. You might, however, find this example or this one helpful.
Good luck!
1 Abstract Base Class
I've had some fun cases where a parameter is something like ref Guid parent and the corresponding documentation says:
"Pointer to a GUID specifying the parent. Pass a null pointer to use [insert some system-defined item]."
If null (or IntPtr.Zero for IntPtr parameters) really is an invalid parameter, then you're fine using a ref parameter - maybe even better off since it's extra clear exactly what you need to pass.
If null is a valid parameter, you can pass ClassType instead of ref StructType. Objects of a reference type (class) are passed as a pointer, and they allow null.
No, you cannot overload SendMessage and make the wparam argument an int. That will make your program fail on a 64-bit version of the operating system. It has to be a pointer, either IntPtr, a blittable reference or an out or ref value type. Overloading the out/ref type is otherwise fine.
EDIT: As the OP pointed out, this is not actually a problem. The 64-bit function calling convention passes the first 4 arguments through registers, not the stack. There is thus no danger of stack mis-alignment for the wparam and lparam arguments.
I don't see any drawbacks.
By-ref is often enough for simple type and simple structure.
IntPtr should be favored if the structure has a variable-size or if you want to do custom processing.
Using ref is simpler and less error-prone than manipulating pointers manually, so I see no good reason for not using it... Another benefit of using ref is that you don't have to worry about freeing unmanaged allocated memory

Categories