|
Joined: Oct 2005
Posts: 827
Hoopy frood
|
OP
Hoopy frood
Joined: Oct 2005
Posts: 827 |
void sendmirc(HWND hwnd, const char *data) {
HANDLE hmap, hmutex;
char *ptr;
// create the filemapping object
if ((hmap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
0, 1024, "mIRC")) == NULL) return;
// at this point we SHOULD check GetLastError() to see if it's set to
// ERROR_ALREADY_EXISTS; that's how mIRC prescribes that it must be done,
// and if all DLL coders did this, we would not need a mutex at all.
// map the shared memory onto our address space
if ((ptr = (char *)MapViewOfFile(hmap, FILE_MAP_ALL_ACCESS, 0, 0, 0)) == NULL) {
CloseHandle(hmap);
return;
}
// the creation and mapping of the virtual file done so far, can not lead to
// synchronization problems on itself; we only need to protect actual access
// so, create or open a mutex that will protect the shared memory access
if ((hmutex = CreateMutex(NULL, FALSE, "mIRC_SendMessage")) != NULL) {
// wait for this mutex to be released - if it is currently locked, wait!
WaitForSingleObject(hmutex, INFINITE);
// now that we hold the mutex, we can access the shared memory safely
lstrcpyn(ptr, data, 4096);
// and tell mIRC to execute our command
SendMessage(hwnd, WM_USER + 200, 0, NULL);
// release and destroy the mutex
ReleaseMutex(hmutex);
CloseHandle(hmutex);
}
// unmap and close the filemapping
UnmapViewOfFile(ptr);
CloseHandle(hmap);
}
I changed the 1024 to 4096 for it to hold more data in ptr problem im having is im making the c++ open like 20/30 sockets and im getting information of these sockets and sending them to mIRC, sometimes it sends me back the full 30 results (in the form of echos), and sometimes it doesnt echo anything im thinking that sometimes it doesnt echo anything, is this to do with the mutex to the mapped file? i thought the mutex would be released upon a certain amount of time - milisecs? ive been using this function for a while now and thats my only problem, when i open the sockets (by calling a mirc alias which runs a dll function) it echos me results, then when i run the alias again, for e.g., 2 secs later, it wont echo anything
|
|
|
|
Joined: Apr 2004
Posts: 871
Hoopy frood
|
Hoopy frood
Joined: Apr 2004
Posts: 871 |
I changed the 1024 to 4096 for it to hold more data in ptr Don't do that, you're now copying up to 4k characters onto a 1k file mapping object, and mIRC doesn't process commands of more than about 950 characters anyway. This may in fact well be the cause of your problems. problem im having is im making the c++ open like 20/30 sockets and im getting information of these sockets and sending them to mIRC, sometimes it sends me back the full 30 results (in the form of echos), and sometimes it doesnt echo anything
im thinking that sometimes it doesnt echo anything, is this to do with the mutex to the mapped file? i thought the mutex would be released upon a certain amount of time - milisecs? No. The mutex is locked while the filemapping is in use by a thread (i.e. while mIRC is executing the command), and released immediately after. While it is in use by one thread, all other threads will wait until it's released again, and then the next thread gets access, and so on. There is no timeout or anything, and there shouldn't be, either. ive been using this function for a while now and thats my only problem, when i open the sockets (by calling a mirc alias which runs a dll function) it echos me results, then when i run the alias again, for e.g., 2 secs later, it wont echo anything I'm sorry but all this is not nearly enough information for me to tell what's going on..
Saturn, QuakeNet staff
|
|
|
|
Joined: Oct 2005
Posts: 827
Hoopy frood
|
OP
Hoopy frood
Joined: Oct 2005
Posts: 827 |
Ok, e.g..
alias blah {
var %x 1
while (%x <= 20) {
$dllcall(blah.dll, $null, function, datahere)
inc %x
}
}
}
So it calls it multi threaded 20 times...
int WINAPI function(HWND, HWND, char *data, char *parms, BOOL, BOOL) {
//connect socket
//blah blah
// get socket data blah blah
char toSend[256];
strcpy(toSend, "Socket data: ");
strcat(toSend, dataa);
sendmirc(toSend);
}
when i do /blah it echos 20 times - perfect.. but if i do it again 1/2 secs later, it doesnt echo anything, i.e sendmirc doesnt do anything if i restart mirc, then it works fine - echos normal! In the 'function code' i tested it and when i run /blah 1/2 secs later it does everything in the 'function' except fails on the 'sendmirc'
Last edited by pouncer; 09/06/06 10:49 AM.
|
|
|
|
Joined: Apr 2004
Posts: 871
Hoopy frood
|
Hoopy frood
Joined: Apr 2004
Posts: 871 |
I don't see what could be going wrong there; could you please send all the code you're using to the e-mail address in my profile, so that I can compile it and try to reproduce the problem here?
Saturn, QuakeNet staff
|
|
|
|
|