Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

How to use Intptr as BYTE* or HANDLE*

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mariam - 01 Apr 2008 18:39 GMT
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[i] = 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
Fredo - 02 Apr 2008 02:18 GMT
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.

> Hi all
> I have a question about Intptr and how I can use it
[quoted text clipped - 33 lines]
>
> Thanks in Advance
Mariam - 02 Apr 2008 19:34 GMT
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[i] = 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
[quoted text clipped - 77 lines]
>>
>> Thanks in Advance

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.