I have the following function:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, SPoint lParam);
It keeps complaining about Pinvoke stack imbalance when the following code executes:
SendMessage(EventRichTextBox.Handle, EM_GETSCROLLPOS, 0, OldScrollPoint);
What can cause that issue?
this is my SPoint
private struct SPoint
{
public Int32 x;
public Int32 y;
}
and
SPoint OldScrollPoint = default(SPoint);
Can't say for sure, but one obvious possibility is that you are on a 64 bit machine and int is the wrong type for wParam. It needs to be a 64 bit value in a 64 bit process.
We also have no idea how you have declared SPoint. You are meant to pass a pointer to a POINT struct. It doesn't look as though you have done that.
The correct signature is:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(
IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
Your edit clarifies that SPoint is a struct. This then is clearly wrong. You could simply pass the SPoint as an out parameter. That would be the simplest solution.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(
IntPtr hWnd, int Msg, IntPtr wParam, out SPoint lParam);
If you wanted a more general SendMessage signature then you should use IntPtr as I stated above and use Marshal.StructureToPtr.
The wParam argument should be IntPtr. But that's not what triggers the MDA, lying about the argument type is fine but you do have to do it correctly. Structures are passed by reference in the Windows api, declare the lParam argument as ref SPoint. Or out if the structure is returned, the case for EM_GETSCROLLPOS.
Related
From here, we know if malloc_logger global function is defined, it will be called whenever there is a malloc or free operation. I want to use it to record memory allocations in my app like this:
typedef void(malloc_logger_t)(uint32_t type,
uintptr_t arg1,
uintptr_t arg2,
uintptr_t arg3,
uintptr_t result,
uint32_t num_hot_frames_to_skip);
extern malloc_logger_t *malloc_logger;
void my_malloc_stack_logger(uint32_t type, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t result, uint32_t num_hot_frames_to_skip);
malloc_logger = my_malloc_stack_logger;
void my_malloc_stack_logger(uint32_t type, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t result, uint32_t num_hot_frames_to_skip)
{
// do my work
}
In my_malloc_stack_logger, I can directly get the allocated size and address. But how about object types? I want to record the class name if it is an NSObject instance. Is it possible to get this information?
After playing around with the hook, it looks like what you want to achieve is not quite possible.
First problem here is that if you try to read a class name from within this function (by calling any of object_getClassName, class_getName
or NSStringFromClass), this action on its own tends to trigger new allocations. That apparently happens because some Cocoa classes load lazily. I noticed however that when requesting all classes with objc_getClassList it makes a lot of preliminary allocations that helps to avoid them later on. So my idea is to cache all class names before subscribing to the allocations hook and refer to the cached values when needed. For the storage I used Apple's CFMutableDictionary:
CFMutableDictionaryRef objc_class_records;
void refresh_objc_class_list(void) {
pthread_mutex_lock(&objc_class_records_mutex);
if (objc_class_records) {
CFRelease(objc_class_records);
}
objc_class_records = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, &kCFTypeDictionaryValueCallBacks);
// The buffer needs to accomodate at least 26665 instances
static const unsigned buffer_length = 100000;
Class registered_classes[buffer_length];
objc_getClassList(registered_classes, buffer_length);
for (unsigned i = 0; i < buffer_length; ++i) {
if (!registered_classes[i]) {
break;
}
const Class class = registered_classes[i];
const CFStringRef class_name = CFStringCreateWithCString(kCFAllocatorDefault, class_getName(class), kCFStringEncodingUTF8);
CFDictionarySetValue(objc_class_records, class, class_name);
CFRelease(class_name);
}
}
Be advised that you don't want to have it called when the malloc logger is enabled (especially from within the hook itself).
Now you need to obtain a Class instance from the Objective-C objects. Depending on the type of allocation, the pointer argument goes to fifth or third parameter:
void my_malloc_logger(uint32_t type, uintptr_t param0, uintptr_t param1, uintptr_t param2,
uintptr_t param3, uint32_t frames_to_skip) {
void *ptr = NULL;
unsigned size = 0;
switch (type) {
case MALLOC_OP_MALLOC:
case MALLOC_OP_CALLOC:
ptr = (void *)param3;
size = (unsigned)param1;
break;
case MALLOC_OP_REALLOC:
ptr = (void *)param3;
size = (unsigned)param2;
break;
case MALLOC_OP_FREE:
ptr = (void *)param1;
break;
}
id objc_ptr = (id)ptr;
Class objc_class = object_getClass(objc_ptr);
if (!objc_class) {
return;
}
const CFStringRef class_name;
const bool found = CFDictionaryGetValueIfPresent(objc_class_records, objc_class, (const void **)&class_name);
if (found) {
const static unsigned name_max_length = 256;
char c_class_name[name_max_length];
if (CFStringGetCString(class_name, c_class_name, name_max_length, kCFStringEncodingUTF8)) {
const char *alloc_name = alloc_type_name(type);
nomalloc_printf_sync("%7s: Pointer: %p; Size: %u; Obj-C class: \"%s\"\n", alloc_name, objc_ptr, size, c_class_name);
}
}
}
And now why it won't work as expected:
object_getClass is not able to tell whether a pointer is an object of Cococa classes at the time of allocation (it will find the class, however, when the object is already allocated, e.g. before deallocation). Thus, the following code:
[NSObject new];
Will produce output similar to this:
CALLOC: Pointer: 0x600000600080; Size: 16
FREE: Pointer: 0x600000600080; Size: 0; Obj-C class: "NSObject"
Most of the standard Cocoa classes are in fact so-called Class Clusters and under the hood the actual allocation happens for an instance of a private class (which is not always recognisable by its public interface), thus this information is incomplete and sometimes misleading.
There are also many other factors which need to be taken into account (which i didn't cover here because it's beyond the question asked): the way you output data to standard output should not cause allocation by itself; the logging needs synchronisation since allocation happens a lot from any number of threads; if you want to enable/disable recording the Objective-C classes (or update the cache occasionally) access to the storage also needs to be synchronised.
Having that said if you are satisfied with what can be done with it, feel free to refer to the repository I made where this approach is already implemented in a form of a static library.
I have a c application and a java application. I communicate with jna to get results from the c program.
I Have a jna-Callback function:
public LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WinUserConstants.WM_GRAB_BASE:
System.out.println("WM_GRAB_BASE");
return new LRESULT(1);
case WinUserConstants.WM_GRAB_TRANSFER_FINISHED:
System.out.println("WM_GRAB_TRANSFER_FINISHED");
return new LRESULT(1);
case WinUserConstants.WM_GRAB_IMAGE_SAVED:
System.out.println("WM_GRAB_IMAGE_SAVED");
return new LRESULT(1);
default:
return User32.INSTANCE.DefWindowProc(hWnd, uMsg, wParam, lParam);
}
I need the WPARAM variable. I cant change the WPARAM to String. If i do that my window shows no controls.
WPARAM is defined as "HGLOBAL on memory containing (wchar_t *)filename". I need that filename and cant access the c code.
You can use WPARAM.toPointer().getWideString(0) to extract the string.
toPointer() effectively "casts" the WPARAM to a pointer value, from which you can then extract the (wide) native C string.
http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c10849/Setting-a-System-Environment-Variable.htm
SendMessage( HWND_BROADCAST , WM_SETTINGCHANGE , 0 , (LPARAM) "Environment" );
JNA and windows xp: call to notify that Environment has been changed
see link: twall.github.com/jna/3.5.1/javadoc/
see link: twall.github.com/jna/3.5.1/javadoc/com/sun/jna/platform/win32/User32.html
PostMessage(WinDef.HWND hWnd, int msg, WinDef.WPARAM wParam, WinDef.LPARAM lParam)
This function places a message in the message queue associated with the thread that created the specified window and then returns without waiting for the thread to process the message.
import com.sun.jna.*;
import com.sun.jna.win32.*;
import com.sun.jna.platform.win32.*;
import com.sun.jna.ptr.*;
public class MainJNA {
public static void main (String [] args){
String myString = "Environment";
Pointer myPointer = new Memory(myString.length()+1);
myPointer.setString(0,myString);
Pointer HWND_BROADCAST = new Pointer(0xFFFF);
int msg = 0x001A; // WM_SETTINGCHANGE = WM_WININICHANGE = 0x001A
WinDef.HWND hWnd = new WinDef.HWND( HWND_BROADCAST );
WinDef.WPARAM wParam = new WinDef.WPARAM(0);
WinDef.LPARAM lParam = new WinDef.LPARAM( myPointer.getLong(0) );
// Exception in thread "main" java.lang.IllegalArgumentException:
// Argument value 0x6d6e6f7269766e45 exceeds native capacity (4 bytes)
// mask=0xffffffff00000000
User32 user32 = (User32) Native.loadLibrary(
"user32" , User32.class , W32APIOptions.DEFAULT_OPTIONS );
user32.PostMessage( hWnd , msg , wParam , lParam );
}
} // end of class MainJNA
How to pass String parameter "Environment" to user32.PostMessage ???
And not to get Exception in thread "main" java.lang.IllegalArgumentException: Argument value 0x6d6e6f7269766e45 exceeds native capacity (4 bytes) mask=0xffffffff00000000
Thx
You're getting that error because you're trying to write a 64-bit value (myPointer.getLong(0)) into a 32-bit container (LPARAM).
You already have the pointer value you need for the LPARAM in myPointer; the recommended way to "cast" the pointer to LPARAM is to simply declare a version of PostMessage which takes an appropriately-typed fourth argument, e.g.
void PostMessage(WinDef.HWND hWnd, int msg, WinDef.WPARAM wParam, Pointer lParam);
void PostMessage(WinDef.HWND hWnd, int msg, WinDef.WPARAM wParam, String lParam);
This is preferable and more type-safe than manually converting between disparate types (i.e. from String or Pointer to an integer type).
I am novice jn JNA and am getting a bit confused with mixture of java and C++. In the WNDPROC callback method, the LPARAM is sent to the callback has to be used to retrive the DEV_BROADCAST_DEVICEINTERFACE class object. This is my code:
public static User32.WNDPROC WndProc = new User32.WNDPROC() {
#Override
public LRESULT callback(HWND hWnd, int uMSG, WPARAM uParam, LPARAM lParam)
{
User32.DEV_BROADCAST_DEVICEINTERFACE b = (User32.DEV_BROADCAST_DEVICEINTERFACE) lParam;
if(b != null){
System.out.println("Device Name: " + b.dbcc_name.toString ()); System.out.println("New Volume GUID:" + b.dbcc_classguid.toString());
}
}
The compiler begins to complain when I try to convert the lParam to class object, for obvious reasons. How do I achieve this?
You don't have to use LPARAM; if you're being passed a structure (or other specific type) by the native code, you can define the appropriate method signature and JNA will do the right thing, converting the native value into something useful in Java.
public LRESULT callback(HWND hWnd, int uMSG, WPARAM uParam, User32.DEV_BROADCAST_DEVICEINTERFACE lParam);
I want to add reference to DNSSDObjects to a project in MonoTouch, specifically DNSServiceResolve object.
I want to access DNSServiceResolve in a MonoTouch project but cant find that class anywhere.
How can that be done ?
I got the functions from dns_sd.h working with P/Invokes. Most of the definitions were already done in the project zeroconfignetservices [1], specifically in the file mDNSImports.cs. Instead of referencing dnssd.dll, it is /usr/lib/system/libsystem_dnssd.dylib on iOS.
So for example, the definition for DNSServiceQueryRecord would be:
[DllImport("/usr/lib/system/libsystem_dnssd.dylib")]
public static extern DNSServiceErrorType DNSServiceQueryRecord(out IntPtr sdRef,
DNSServiceFlags flags,
UInt32 interfaceIndex,
[MarshalAs(
UnmanagedType.CustomMarshaler,
MarshalTypeRef = typeof(Utf8Marshaler))] String fullname,
DNSServiceType rrType,
DNSServiceClass rrClass,
DNSServiceQueryReply callBack,
IntPtr context);
And a query for an SRV record would be as follows:
public void DoDnsLookup()
{
IntPtr sdRef;
var result = DNSServiceQueryRecord(
out sdRef,
DNSServiceFlags.LongLivedQuery,
0,
"_xmpp-client._tcp.gmail.com",
DNSServiceType.SRV,
DNSServiceClass.IN,
DnsServiceQueryReply,
IntPtr.Zero
);
if (result == DNSServiceErrorType.NoError)
{
DNSServiceProcessResult(sdRef);
DNSServiceRefDeallocate(sdRef);
}
}
//see [2] why this method is static and the attribute
[MonoPInvokeCallback(typeof(DNSServiceQueryReply))]
public static void DnsServiceQueryReply(
IntPtr sdRef,
DNSServiceFlags flags,
UInt32 interfaceIndex,
DNSServiceErrorType errorCode,
[MarshalAs(
UnmanagedType.CustomMarshaler,
MarshalTypeRef = typeof(Utf8Marshaler))] String fullname,
DNSServiceType rrType,
DNSServiceClass rrClass,
UInt16 rdLength,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 7)]byte[] rData,
UInt32 ttl,
IntPtr context)
{
if (result == DNSServiceErrorType.NoError)
{
// process returned DNS data in rData
// a useful library for this could be Bdev.Net.Dns [3]
}
}
All the classes, enums, etc. not defined here are from [1].
References:
http://code.google.com/p/zeroconfignetservices
http://docs.xamarin.com/ios/guides/advanced_topics/limitations#Reverse_Callbacks
dnslookup.codeplex.com