How to use Intptr as BYTE* or HANDLE*

M

Mariam

Hi all
I have a question about Intptr and how I can use it

I have a C++ code which do the following:

DWORD nBufferSize = 0;

BYTE *pBuffer = NULL;

hr =GetInfo(&nBufferSize, &pBuffer, NULL);

to use the function GetInfo in C# I have to pass the parameter pBuffer as
Intptr, I don't know how to initiliaze it to act as a BYTE*?

The same problem I have with type HANDLE , e C++ code is as the follwoing

HANDLE *pSampleReadyEvents = NULL;

pSampleReadyEvents =
(HANDLE*)AllocateMemory(sizeof(HANDLE) * 100);


for (DWORD i = 0; i < 100; i++)
{

pSampleReadyEvents = CreateEvent(NULL, FALSE, FALSE, NULL);

}
DWORD nWaitCode = WaitForMultipleObjects(nNumberOfSamples,
pSampleReadyEvents,
FALSE,
INFINITE);

IN C# the methods CreateEvent & WaitForMultipleObjects treat
pSampleREadyEvents as Intptr, I also don't know how can I create it to work
well?

Thanks in Advance
 
M

Mariam

First at all thanks alot for your help and quick reply, I really appreciate
it



I still have some problems here



1- About WaitForMultipleObjects
-------------------------------------->



when I try to dll import kerenel32.dll I always get that error :-
An exception of type 'System.DllNotFoundException' occurred in
WindowsApplication4.exe but was not handled in user code



Additional information: Unable to load DLL 'kernel32.dll, SetLastError=true
': The specified module could not be found. (Exception from HRESULT:
0x8007007E)



I tried to warraper the C++ project I have inside a dll to call the
fucntions waitofrmultipleobjects and createvent from there as the following
:-



extern "C" __declspec( dllexport ) DWORD M_WaitForMultipleObjects(DWORD
nNumberOfSamples, HANDLE *pSampleReadyEvents)



{



DWORD nWaitCode = WaitForMultipleObjects(nNumberOfSamples,



pSampleReadyEvents,



FALSE,



INFINITE);



return nWaitCode;



}



extern "C" __declspec( dllexport )HANDLE M_CreateEvent()



{



HANDLE SampleEvent= CreateEvent(NULL, FALSE, FALSE, NULL);



return SampleEvent;



}



and In my C# code I called it as:-



[DllImport("TapiRcevDll.dll")]



unsafe public static extern Int32 M_WaitForMultipleObjects(Int32
nNumberOfSamples, IntPtr[] PSampleReadyEvenets);



[DllImport(TapiRcevDll.dll")]



unsafe public static extern IntPtr M_CreateEvent();



IntPtr[] ReadySampleEvents = new IntPtr[NUmberOfSamples];



for (int i = 0; i < NUmberOfSamples; i++)



{



ReadySampleEvents = M_CreateEvent();//CreateEvent((IntPtr)null, false,
false, (string)null);



}







Int32 WaitCode =
M_WaitForMultipleObjects(Convert.ToUInt32(NNumberOfSamples),
ReadySampleEvents, false,Convert.ToUInt32(4294967295));



but the WaitCode always return -1, I don't know why



---------------------------------------------------------------------------------------------------------------







2- About the GetInfo function
--------------------------------->



Yeah I didn't describe it or waht it do beacuase I didn't want to go into
details, but it seems that I have to



get info actually is a function inside the directshow library, I use it to
get memory info about audio object



int eh C++ code it is called



BYTE *pBuffer = NULL;



pSampleMemoryData->GetInfo(



&nBufferSize, //Length of memory in byt



&pBuffer, // Pointer to the memory



NULL//Length of data in bytes



);



pSampleMemoryData is object from IMemoryData interface in DirectShow



I will use this pBuffer then to read a file in it using avi



The problem exists that I am using a warraper for directshow (directshow.net
2005)



the function GetInfo in it takes parameters as follow



GetInfo(out int nBufferSize, IntPtr pBuffer,out int pActualdata)



So,I can't pass pBuffer as byte array as u advised me , I don't know how to
pass this pBuffer as Intptr and act as Byte array



Thats all



sorry for this so long mail, hope I didn't bother you,but really my work
stops now on this two points



Thanks in Advance


I have to say, this is what I love about Interop in C#. It's so easy, you
simply can't believe it. You don't state which GetInfo() method you're
calling, but basically, type the stuff in C# however you want, as long as
it's mildly compatible, it seems C# will figure out the right way to
marshal the data. Since I don't know what the C++ side of the GetInfo
definition looks like, I'll go with the WaitForMultipleObjects as an
example.

In C/C++, you have:

DWORD WaitForMultipleObjects(DWORD, const HANDLE *, BOOL, DWORD)

So, in C#, you can do several things. DWORD would be compatible with uint,
but you could also use int (though that might cause problems when you get
above 2 billion objects or 2 billion milliseconds)

But let's keep it clean and use UINTs. Simply define it as:

[DllImport("kernel32.dll")]
public static uint WaitForMultipleObjects(uint count, IntPtr[] handles,
bool waitAll, uint milliseconds);

Voila, now just pass a bunch of IntPtrs and the framework will handle the
marshalling. Just allocate your array of IntPtrs and pass them.

You could probably make the handles parameter int[]. Wouldn't matter, as
long as the ints were actual handle values. The marshalling code would
make the necessary conversions. Still, best to go with the closest thing.

Just to give you an example of how flexible this, in one app I work on, we
have something like 10 overrides of SendMessage, each with variations of
the wParam and lParam parameters. Some are typed as structs, others are
ints or uints. It just depends on what we need to send.

For the GetInfo, I don't know what your third parameter is, so I'm going
to just do a version with the first 2 parameters to show you how that
would be done:

[DllImport("whatever.dll")] <--- Replace "whatever" with whatever the dll
is that the method belongs to.
public static uint GetInfo(ref uint bufferSize, out byte[] buffer)

The marshalling code will do the rest.





Mariam said:
Hi all
I have a question about Intptr and how I can use it

I have a C++ code which do the following:

DWORD nBufferSize = 0;

BYTE *pBuffer = NULL;

hr =GetInfo(&nBufferSize, &pBuffer, NULL);

to use the function GetInfo in C# I have to pass the parameter pBuffer as
Intptr, I don't know how to initiliaze it to act as a BYTE*?

The same problem I have with type HANDLE , e C++ code is as the follwoing

HANDLE *pSampleReadyEvents = NULL;

pSampleReadyEvents =
(HANDLE*)AllocateMemory(sizeof(HANDLE) * 100);


for (DWORD i = 0; i < 100; i++)
{

pSampleReadyEvents = CreateEvent(NULL, FALSE, FALSE, NULL);

}
DWORD nWaitCode = WaitForMultipleObjects(nNumberOfSamples,
pSampleReadyEvents,
FALSE,
INFINITE);

IN C# the methods CreateEvent & WaitForMultipleObjects treat
pSampleREadyEvents as Intptr, I also don't know how can I create it to
work well?

Thanks in Advance

 

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