|
Hi,
I tried to raise an event from unmanaged code class to a class in managed code.
I passed a delegate pointer (using Marshal) to the unmanaged class but I got an error when the unmanaged class tried to execute the function.
I tried to use pin_ptr to the delegate pointer with no results.
How can I raise event from unmanaged code class to a managed code class using delegate (managed) and function pointer(unmanaged)?
Thanks
|
|
|
|
|
I don't think that it is possible to raise event directly from unmanaged code, but it is possible to call managed callback function which does this. Use Marshal.GetFunctionPointerForDelegate method to pass managed callbak function address to unmanaged code.
Keep delegate instance alive all time when callback function can be called.
|
|
|
|
|
Thanks for your reply,
I've a delegate instance that lives all the time and I sent it to the unmanaged code using Marshal.GetFunctionPointerForDelegate.
But I think there is a problem with the GC since it can be reallocated the delegate on the heap.
How can it be handle?
Thanks
|
|
|
|
|
I think that that delegate is pinned internally by this function. .NET doesn't allow to take unmanaged pointers to managed unpinned objects.
You can do this manually using GCHandle, as shown in MSDN topic "GCHandle.Alloc Method (Object)", but I think this is exactly what GetFunctionPointerForDelegate does.
|
|
|
|
|
How to find the pointer is initialized or not.
In destructor, if I delete the pointer array like:
if(ptr)
{
delete [] ptr;
}
an exception is got inside catch(...){}.
The if condition is sucess even if the pointer is not initialized.
The pointer address is 0xcccccccc.
Thanks & Regards,
Suman
|
|
|
|
|
if(ptr)
{
delete [] ptr;
}
is totally bad C++ code.
just do:
delete [] ptr;
because if ptr==NULL, you delete it without any problem.
for code like above, you must init at begining like:
ptr=0;
if the ptr will be reused, you must init after delete like:
delete []ptr;
ptr=0;
|
|
|
|
|
Always init your pointers to NULL (0xcccccccc is what the debugger sets uninitialized memory/variables to).
If a pointer's value is NULL, then it's not pointing to anything.
If the value is anything else, it might point to valid memory, or it might not. There is no foolproof way to tell.
|
|
|
|
|
Thanks for help!
In release mode there is no error caught by catch() when deleting unintialized pointer.
I think there is no fixed value for unintialized pointer in release mode.
Is there any chance of deleting the memory of other variable or function when deleting the unintialized pointer?
Is it possible to know the address is some other variable / function's memory.
Thanks & Regards,
Suman
|
|
|
|
|
It's your responsibility to make sure any pointer that you dereference is pointing to valid memory. That's why C++ guys make the big bucks
|
|
|
|
|
Thank you very much for your great help!
Wishing you a Very Happy and Prosperous New Year 2007!!
Thanks & Regards,
Suman
|
|
|
|
|
IsBadReadPtr & IsBadWritePtr may help you by indicating if the pointer is readable and/or deletable.
Possibly also HeapWalk could help.
|
|
|
|
|
IsBadReadPtr() and IsBadWritePtr() don't work and can actually cause more problems. See this blog[^] for more
|
|
|
|
|
Sorry about that. I didn't know the internal mechanics of these two APIs, nor that their usage can lead to such disasters.
Then I suppose remains HeapWalk... or also this one contains a hidden treasure as well?
|
|
|
|
|
Thanks a lot for the great help from you all!!
Thanks & Regards,
Suman
|
|
|
|
|
now i want to get some informations about the local Floppy Driver. the information may content Vendor ID, Product ID ... or some thing else~
can anybody give me some hint?
if C/C++ can do this tell me how, if not give me some explaination.
thanks ~~~
|
|
|
|
|
CreateFile to open the floppy device;
DeviceIoControl with codes of choice; may include:
- IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER
- IOCTL_STORAGE_GET_MEDIA_TYPES(EX)
etc.
|
|
|
|
|
i got the answer in the Hardware board...
For a floppy drive? There isn't a way to do this. The Device Manager in Windows doesn't care. As far as I know, most of them come back as a Generic Drive on a Standard Controller. About the only ones that would come back with any information are the old 120MB Super Drives.
Dave Kreskowiak
Microsoft MVP - Visual Basic
-- modified at 20:19 Tuesday 26th December, 2006
|
|
|
|
|
Dear All,
Dear Mr.Asef (Iran)
I need to control a peripheral device via parallel port (LPT) by programming Visual C++ language but I'm still confused how to do it.
I have down loaded the program written by Mr. Asef (Port Access
By asef.)but it doesn't work properly , I think that may be I have miss something...
Would you please help me and give me your instruction to solve this problem!
my email: bangkokhanoi2007@yahoo.com
Thank you in advance!
Your sincelery,
BK2007
|
|
|
|
|
Check following before you could do any thing :
1. Bios setting in your PC is in EPP/ ECP mode. (SPP is uni directional)
2. You BIDIR bit is set in base+1 port. (MSB of port 3F9 if Parallel port address is 3F8 and so on...);
I think its a pretty easy task.
I dont know whether Iam allowed to provide url links in this message board.
therefore ... no risk...
haribabu
|
|
|
|
|
Hi all.
I have two threads ... one as a sender and the other a receiver for data sent over sockets. But the sockets are being created and handled in a dll ( a VC# dll which has a class named Class1). so when I call these threads I have to pass the object of the class in the dll because the sender and receiver use the same sockets right!!.
Here is what I have in the sender and receiver threads... ( and I dont know a work around for the DoEvents thing, because I have to enforce the processor to do other tasks while the thread is in a while loop .... and I have removed the doevents completely as well and it doesnt make any difference. So the problem is not with doevents).
DWORD WINAPI SenderThread(LPVOID iValue)
{
Mutex^ mut = gcnew Mutex();
mut->WaitOne();
Class1^ senderdll = *((Class1^*)iValue);
mut->Close();
int returnval;
DoEvents();
while(true)
{
returnval = senderdll->SenderEntryPoint();
if(returnval == 1) // sockets closed
{
break;
//dynamic_cast<autoresetevent^>(stateInfo)->Set();
}
}
return 0;
}
DWORD WINAPI ReceiverThread(LPVOID iValue)
{
Mutex^ mut = gcnew Mutex();
mut->WaitOne();
Class1^ receiverdll = *((Class1^*)iValue);
mut->Close ();
int returnval;
DoEvents();
while(true)
{
returnval = receiverdll->ReceiverEntryPoint();
if(returnval == 1) // the sockets closed
break;
}
return 0;
}
and here is how I am creating these threads. (The cmdListen_Click function creates the sockets.)
Class1^ c = gcnew Class1();
c->cmdListen_Click(NewPosition);//call the dll function to create sockets
////Threads start
DWORD dwGenericThread;
hThread2 = CreateThread(NULL,0,SenderThread,&c,0,&dwGenericThread);//the sender thread
if(hThread2 == NULL)
{
DWORD dwError = GetLastError();
return 0;
}
hThread3 = CreateThread(NULL,0,ReceiverThread,&c,0,&dwGenericThread);//the receiver thread
if(hThread3 == NULL)
{
DWORD dwError = GetLastError();
return 0;
}
//////Threads end
Now the two problems I am facing are:
1) How do I synchronize the threads? The AutoResetEvent (as given in http://msdn2.microsoft.com/en-us/library/system.threading.autoresetevent.aspx), uses a threadpool to create threads. My problem is if I use the threadpool instead of using the CreateThread how do I pass the Class1 object in the threads ?
2) My second problem is the AccessViolation error, which I am getting at the point where I create the instance of the Class in the thread. ( in both sender or receiver, depending upon which thread I am creating first !!).
Class1^ receiverdll = *((Class1^*)iValue);
What am I doing wrong here? When I was getting an access violation error in VC#, I used LOCK to enforce resource allocation. How do I do this in VC++?
Thanks.
|
|
|
|
|
Try posting your C++/CLI questions here[^]
|
|
|
|
|
Does anybody know which string would expand to "C:\Windows\Profiles\<username>\ ", I believe this path exists on Win98 & ME.
Or better still, does anybody know of a reference list for these strings? MSDN has come up a little short on this api
|
|
|
|
|
That would be %USERPROFILE%
|
|
|
|
|
Thanks for that. It expands to something different on Vista, and since I don't have access to a 98 machine, it's better to ask :p
|
|
|
|
|