This appears to be the most commonly asked C# interop question and yet seems to be difficult to find a working solution for.
I am in need of allocating an array of matrix datastructure in C# passing it to a C DLL which fills up the data and returns it to the caller to deal with.
Based on various pages on the web, I seem to have managed...
I need to pass an IntPtr to IStream.Read, and the IntPtr should point to a ulong variable. How do I get this IntPtr that points to my ulong variable?
...
All, this is a follow up from a previous question here: http://stackoverflow.com/questions/727942/c-formatting-external-dll-function-parameters
Here specifically is the code that I am trying to convert to C#:
FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pMapping, &PagePerSector);
// Allocate the m...
Hello,
I want to convert message.WParam to Socket.
protected override void WndProc(ref Message m)
{
if (m.Msg == Values.MESSAGE_ASYNC)
{
switch (m.LParam.ToInt32())
{
case Values.FD_READ:
WS2.Receive(m.WParam);
case Values.FD_WRITE: bre...
Hello,
I need to call VirtualAllocEx and it returns IntPtr.
I call that function to get an empty address so I can write my codecave there(this is in another process).
How do I convert the result into UInt32,so I could call WriteProcessMemory() lately with that address?
...
I have a C++ DLL returning an int* to a C# program. The problem is the int* in C# remains null after the assignment.
When I assign the C++ result to an IntPtr, I get a correct non-null value. However any attempt to convert this to an int* results in null.
I've tried:
IntPtr intp = cppFunction (); // Non-null.
int* pi = (i...
I need the intPtr for a form. Control.FromHandle(control) gives me the control from a handle, but I need the opposite--get the handle from a control. How do I do this?
...
Hi,
On the Media Foundation SDK there is the GetPhysicalMonitorsFromHMONITOR function
that I am trying to implement using C# but with no luck ...
In the returned PHYSICAL_MONITOR[], the function returns the string description of the monitor but for some mysterious reasons, the hPhysicalMonitor handle remains at 0.
I have generated the...
I'm updating an old c++ service to use WCF instead of RPC and there is an issue as to what type to use when sending and receiving a handle (HANDLE, void*..etc). In the updated service I currently have it using IntPtr, but this does not work when going from a 64 bit version of the service to a 32 bit version. The IntPtr can not deserializ...
Hi,
I am using dllImport to use a C library in C# .NET. One of the methods in this library uses data type void* as parameter. I found out, that I can use the data type IntPtr in C# matching the void*.
Now I simply don't know how to set the value of this IntPtr parameter. In fact I want to put a float value into this parameter. How wou...
We're in a strange situation with a legacy winforms VB.NET 1.1 application using ASMX web services. Trying to send a user Token from a WindowsIdentity object as a parameter to a WebMethod. I will be adding a 'HACK: comment.
System.Security.Principal.WindowsIdentity.GetCurrent().Token
The token is of type IntPtr, the first problem is t...
Through using IntelliSense and looking at other people's code, I have come across this IntPtr type; every time it has needed to be used I have simply put null or IntPtr.Zero and found most functions to work. What exactly is it and when/why is it used?
...
I was just looking at an example, and in it I saw the code
return new IntPtr(handle);
After poking around our code, I found that we have already used a similar pattern, but in our code we had almost the same thing:
return (IntPtr)handle;
Is there a difference between those two takes? Will the second one be "better" in any way, ...
I made this from an example i saw, it never threw any error, but the image is displayed as grey.
Is there a better way to do this?
private unsafe void menuItem7_Click(object sender, EventArgs e)
{
var settings = Utility.GatherLocalSettings();
openFileDialog1.InitialDirectory = settings.SavePath;
openFileDia...
I have the following C++ function definition, which I am trying to call through PInvoke from managed code:
bool FooBar(SIZE_T* arg1);
My managed declaration looked as follows:
[DllImport("mydll", SetLastError=true, CharSet=CharSet.Unicode)]
private static extern bool FooBar(ref uint arg1);
Some of you may notice the same bug I even...
I tried to allocate an array of structs in this way:
struct T {
int a; int b;
}
data = Marshal.AllocHGlobal(count*Marshal.SizeOf(typeof(T));
...
I'd like to access to allocated data "binding" a struct to each element in array allocated
with AllocHGlobal... something like this
T v;
v = (T)Marshal.PtrToStructure(data+1, typeof(T))...
I have a class that will have a few instances persistent throughout the duration of the application. These objects will each need to call a dll method that appends data from an existing float[] buffer, and passes the full dataset to a DLL method that accepts an IntPtr (float array), several times per second. Is it better to do it as un...
I am receiving an IntPtr and an int specifying the number of bytes it points to. The data can contain any characters including null, EOL, etc. When trying the following, the buffer is corrupted:
//buffer is the IntPtr
//count is the number of bytes in 'buffer'
byte[] test = new byte[count];
Marshal.Copy(buffer, test, 0, count);
In...
So from what I can tell, every managed example of IntPtr addition I have found is WRONG.
For example:
http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/10/16/10987.aspx
My thought being, that if IntPtr is at (or near) int32.MaxValue on a 32-bit system, and you add an offset which overflows int32, isn't that still a valid memor...
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...