Go Back   FlashFXP Forums > >

Programming Need help with C/C++/Delphi? Ask here and make us all laugh.

Closed Thread
 
Thread Tools Rate Thread Display Modes
Old 10-29-2003, 04:04 AM   #1
darkone
Disabled
FlashFXP Registered User
ioFTPD Administrator
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default [c] DataCopy functions for dummies

k.. not for dummies, but it makes life much easier (+ you can choose between FILEMAP & SHELL modes.. [filemap is available in next beta])

#include <ioFTPD.h>

/*

Prototypes of some undocumented winapi functions

HANDLE WINAPI SHAllocShared(LPVOID Buf, ULONG BufSize, DWORD PID);
BOOL WINAPI SHFreeShared(HANDLE hMemory, DWORD PID);
LPVOID WINAPI SHLockShared(HANDLE hMemory, DWORD PID);
BOOL WINAPI SHUnlockShared(LPVOID Buf);

*/


typedef struct _LOCALDATA
{
HWND hDaemon;
DWORD dwProcessId;
DWORD dwType;

HMODULE hShellLibrary;
HANDLE hHeap;
LPVOID ShellAlloc;
LPVOID ShellLock;
LPVOID ShellUnlock;
LPVOID ShellFree;

} LOCALDATA, * LPLOCALDATA;

typedef struct _ALLOCATION
{
LPDC_MESSAGE lpMessage;
LPVOID lpMemory;
LPVOID hDaemon;
HANDLE hObject;
HANDLE hEvent;
DWORD dwBytes;

} ALLOCATION, * LPALLOCATION;



BOOL InitializeLocalData(LPLOCALDATA lpData, DWORD dwType)
{
// Initialize data structure
lpData->dwProcessId = GetCurrentProcessId();
lpData->dwType = dwType;
lpData->hDaemon = FindWindow("ioFTPD::MessageWindow", NULL);
lpData->hHeap = GetProcessHeap();

if (dwType == SHELL)
{
// Load shell library
lpData->hShellLibrary = LoadLibrary("shell32.dll");

if (lpData->hShellLibrary)
{
lpData->ShellAlloc = GetProcAddress(lpData->hShellLibrary, "SHAllocShared");
lpData->ShellFree = GetProcAddress(lpData->hShellLibrary, "SHFreeShared");
lpData->ShellUnlock = GetProcAddress(lpData->hShellLibrary, "SHUnlockShared");
lpData->ShellFree = GetProcAddress(lpData->hShellLibrary, "SHLockShared");
}
else return FALSE;
}
else lpData->hShellLibrary = NULL;

return (lpData->hDaemon ? TRUE : FALSE);
}


VOID DeleteLocalData(LPLOCALDATA lpData)
{
if (lpData->hShellLibrary) FreeLibrary(lpData->hShellLibrary);
lpData->dwType = (DWORD)-1;
}



LPALLOCATION SharedAllocate(BOOL bCreateEvent, DWORD dwBytes, LPLOCALDATA lpData)
{
LPALLOCATION lpAllocation;
LPDC_MESSAGE lpMessage;
DWORD dwMessage;
HANDLE hObject, hEvent;
LPVOID hRemote;
BOOL bError;

bError = TRUE;
hEvent = NULL;
hObject = NULL;
lpMessage = NULL;

if (! dwBytes) return FALSE;
if (bCreateEvent &&
! (hEvent = CreateEvent(NULL, FALSE, FALSE, NULL))) return NULL;

lpAllocation = (LPALLOCATION)HeapAlloc(lpData->hHeap, 0, sizeof(ALLOCATION));
if (! lpAllocation)
{
if (hEvent) CloseHandle(hEvent);
return NULL;
}

dwBytes += sizeof(DC_MESSAGE);
// Allocate memory for local process
switch (lpData->dwType)
{
case SHELL:
hObject = ((HANDLE (WINAPI *)(LPVOID, ULONG, DWORD))lpData->ShellAlloc)(NULL, dwBytes, lpData->dwProcessId);
if (hObject) lpMessage = (LPDC_MESSAGE)((LPVOID (WINAPI *)(HANDLE, DWORD))lpData->ShellLock)(hObject, lpData->dwProcessId);
dwMessage = WM_DATACOPY_SHELLALLOC;
break;
case FILEMAP:
hObject = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE|SEC_COMMIT, 0, dwBytes, NULL);
if (hObject) lpMessage = (LPDC_MESSAGE)MapViewOfFile(hObject, FILE_MAP_WRITE|FILE_MAP_READ, 0, 0, dwBytes);
dwMessage = WM_DATACOPY_FILEMAP;
break;
}

if (lpMessage)
{
lpMessage->hEvent = hEvent;
lpMessage->lpContext = &lpMessage[1];
lpMessage->lpMemoryBase = (LPVOID)lpMessage;
lpMessage->dwIdentifier = 0;

// Query ioftpd
SetLastError(0);
hRemote = (LPVOID)SendMessage(lpData->hDaemon, dwMessage, (WPARAM)lpData->dwProcessId, (LPARAM)hObject);

if (hRemote && GetLastError() == NO_ERROR) bError = FALSE;
}

if (bError)
{
// Free resources
switch (lpData->dwType)
{
case SHELL:
if (lpMessage) ((BOOL (WINAPI *)(LPVOID))lpData->ShellUnlock)((LPVOID *)lpMessage);
if (hObject) ((BOOL (WINAPI *)(HANDLE, DWORD))lpData->ShellFree)(hObject, lpData->dwProcessId);
hObject = NULL;
break;
case FILEMAP:
if (lpMessage) UnmapViewOfFile(lpMessage);
break;
}
if (hObject && hObject != INVALID_HANDLE_VALUE) CloseHandle(hObject);
if (hEvent) CloseHandle(hEvent);
HeapFree(lpData->hHeap, 0, lpAllocation);
lpAllocation = NULL;
}
else
{
// Update structure
lpAllocation->hDaemon = hRemote;
lpAllocation->lpMemory = &lpMessage[1];
lpAllocation->hEvent = hEvent;
lpAllocation->hObject = hObject;
lpAllocation->lpMessage = lpMessage;
lpAllocation->dwBytes = dwBytes - sizeof(DC_MESSAGE);
}
return lpAllocation;
}


VOID SharedFree(LPALLOCATION lpAllocation, LPLOCALDATA lpData)
{
// Free resources
switch (lpData->dwType)
{
case SHELL:
((BOOL (WINAPI *)(LPVOID))lpData->ShellUnlock)((LPVOID *)lpAllocation->lpMessage);
((BOOL (WINAPI *)(HANDLE, DWORD))lpData->ShellFree)(lpAllocation->hObject, lpData->dwProcessId);
lpAllocation->hObject = NULL;
break;
case FILEMAP:
UnmapViewOfFile(lpAllocation->lpMessage);
break;
}
if (lpAllocation->hEvent) CloseHandle(lpAllocation->hEvent);
if (lpAllocation->hObject) CloseHandle(lpAllocation->hObject);
SendMessage(lpData->hDaemon, WM_DATACOPY_FREE, 0, (LPARAM)lpAllocation->hDaemon);
HeapFree(lpData->hHeap, 0, lpAllocation);
}



DWORD QueryDaemon(DWORD dwQueryType, LPALLOCATION lpAllocation, DWORD dwTimeOut, LPLOCALDATA lpData)
{
lpAllocation->lpMessage->dwIdentifier = dwQueryType;
PostMessage(lpData->hDaemon, WM_SHMEM, 0, (LPARAM)lpAllocation->hDaemon);
if (dwTimeOut &&
lpAllocation->hEvent)
{
if (WaitForSingleObject(lpAllocation->hEvent, dwTimeOut) == WAIT_TIMEOUT) return (DWORD)-1;
return lpAllocation->lpMessage->dwReturn;
}
// No timeout/event, return value can't be checked
return (DWORD)-1;
}




/*

Main() - Everything is stuffed here

*/
INT main(INT ArgC, CHAR *ArgV[])
{
LPALLOCATION lpAllocation;
LPDC_ONLINEDATA lpOnlineData;
LOCALDATA LocalData;
DWORD dwStartTicks, dwStopTicks, dwUsers;
BOOL bLoop;

bLoop = TRUE;
dwStartTicks = GetTickCount();
dwUsers = 0;

if (InitializeLocalData(&LocalData, FILEMAP))
{
// Allocate memory
lpAllocation = SharedAllocate(TRUE, sizeof(DC_ONLINEDATA) + _MAX_PATH * 2, &LocalData);

if (lpAllocation)
{
lpOnlineData = (LPDC_ONLINEDATA)lpAllocation->lpMemory;
// Init structure
lpOnlineData->iOffset = 0;
lpOnlineData->dwSharedMemorySize = lpAllocation->dwBytes;

for (;bLoop
{
// Send message to daemon
switch (QueryDaemon(DC_GET_ONLINEDATA, lpAllocation, 5000, &LocalData))
{
case (DWORD)-1:
// End/time out/fatal error
bLoop = FALSE;
break;
default:
// Need more memory
lpOnlineData->iOffset++;
break;
case 0:
// Success
if (! lpOnlineData->OnlineData.szRealPath) lpOnlineData->OnlineData.szRealPath = "";
printf("%i: %s\n",
lpOnlineData->OnlineData.Uid, lpOnlineData->OnlineData.szRealPath);
dwUsers++;
}
}
// Free memory
SharedFree(lpAllocation, &LocalData);
}
DeleteLocalData(&LocalData);
}

// Get system tickcount
dwStopTicks = GetTickCount();
// Output process duration
printf("Time wasted: %.3fseconds (%u ticks, %u users)\n",
(dwStopTicks - dwStartTicks) /1000., dwStopTicks - dwStartTicks, dwUsers);
Sleep(10000);


return FALSE;
}
darkone is offline  
Old 10-29-2003, 04:08 AM   #2
darkone
Disabled
FlashFXP Registered User
ioFTPD Administrator
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default

Press quote, if you want tabbed versions version of functions.
darkone is offline  
Old 10-29-2003, 08:09 AM   #3
ADDiCT
Senior Member
FlashFXP Beta Tester
ioFTPD Scripter
 
Join Date: Aug 2003
Posts: 517
Default

This is probably another stupid question of me, but where are WM_DATACOPY_SHELLALLOC, WM_DATACOPY_FREE, SHELL and FILEMAP defined?
I can't find it in any of the header files (i also tried upgrading my includes to the versions of the latest (unregged) ioFTPD rar-sfx, but then i even got more errors )
ADDiCT is offline  
Old 10-30-2003, 04:06 AM   #4
FTPServerTools
Senior Member
FlashFXP Beta Tester
ioFTPD Scripter
 
Join Date: Sep 2002
Posts: 543
Default

Hmm filemapping is indeed easy too... Tho due to compatibility issues I'd prefer shmem. But now I gotta scream: VERSION NUMBER pleeeeeaaaaase.
Its needed for scripters.
FTPServerTools is offline  
Old 10-30-2003, 04:21 AM   #5
darkone
Disabled
FlashFXP Registered User
ioFTPD Administrator
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default

I really suggest moving to memory mapping: it seems faster + it's more reliable (shell allocation routines seem to return weird errors, in some cases)
darkone is offline  
Old 10-30-2003, 09:52 AM   #6
Mouton
Posse Member
Ultimate Scripter
ioFTPD Administrator
 
Join Date: Dec 2002
Posts: 1,956
Default

When we'll have the reply to ADDiCT'S question, we'll be able to compile something. Untill then, we'll have to stick with shmem.
Mouton is offline  
Old 10-30-2003, 10:06 AM   #7
SnypeTEST
Senior Member
ioFTPD Scripter
 
Join Date: Feb 2003
Posts: 458
Default

oooh memory mapping will be much better then I can use it with pb
SnypeTEST is offline  
Old 10-30-2003, 12:55 PM   #8
darkone
Disabled
FlashFXP Registered User
ioFTPD Administrator
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default

you won't be able to use it until next version anyways.. so just define them as something & expect next version of headers to define them for you
darkone is offline  
Old 11-09-2003, 09:50 AM   #9
Harm
Too much time...
Ultimate Scripter
 
Join Date: Jul 2003
Posts: 1,430
Default

Something i've just noticed:

Code:
if (lpData->hShellLibrary)
{
lpData->ShellAlloc = GetProcAddress(lpData->hShellLibrary, "SHAllocShared");
lpData->ShellFree = GetProcAddress(lpData->hShellLibrary, "SHFreeShared");
lpData->ShellUnlock = GetProcAddress(lpData->hShellLibrary, "SHUnlockShared");
lpData->ShellFree = GetProcAddress(lpData->hShellLibrary, "SHLockShared");
}
should be

Code:
if (lpData->hShellLibrary)
{
lpData->ShellAlloc = GetProcAddress(lpData->hShellLibrary, "SHAllocShared");
lpData->ShellFree = GetProcAddress(lpData->hShellLibrary, "SHFreeShared");
lpData->ShellUnlock = GetProcAddress(lpData->hShellLibrary, "SHUnlockShared");
lpData->ShellLock = GetProcAddress(lpData->hShellLibrary, "SHLockShared");
}
Harm is offline  
Old 11-11-2003, 12:06 AM   #10
darkone
Disabled
FlashFXP Registered User
ioFTPD Administrator
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default

True..
darkone is offline  
Closed Thread

Tags
dword, hobject, lpallocation, null;, return

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:46 PM.

Parts of this site powered by vBulletin Mods & Addons from DragonByte Technologies Ltd. (Details)