| View previous topic :: View next topic |
| Author |
Message |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Mon Mar 24, 2008 6:16 am Post subject: [C]Help converting VCL Delphi to pure Win32 API |
|
|
i'm not sure of MSVC++ supports packed record but:
1)How would this look in C ?
| Code: | LongRec = packed record
case Integer of
0: (Lo, Hi: Word);
1: (Words: array [0..1] of Word);
2: (Bytes: array [0..3] of Byte);
end; |
i tried declaring an LPWORD and -> the var but nothing comes out, or at all nothing happens.
2)how do i declare public/private declerations in C ?
thanks !!
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 473
Joined: 09 May 2003 Posts: 25913 Location: The netherlands
|
Posted: Mon Mar 24, 2008 6:26 am Post subject: |
|
|
1:
This comes closest to it:
| Code: |
union LongRec
{
struct
{
unsigned short lo;
unsigned short hi;
} lohi;
unsigned short words[2];
unsigned char bytes[4];
} number;
|
2:
in C (not c++) put your function declarations (and global vars if you dare to use them) in the header file to make them public, and in the C file to keep them private
_________________
Tools give you results. Knowledge gives you control.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Mon Mar 24, 2008 12:35 pm Post subject: |
|
|
db, i wanted to try something new, so i did this:
| Code: | BOOL FileExists(const char *FileName)
{
LPWIN32_FIND_DATAA FindData = NULL;
HANDLE THandle;
THandle = FindFirstFileA(FileName,FindData);
return THandle != INVALID_HANDLE_VALUE;
} |
and when i finds the file i get an error "Debug" or "Close", why?
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Mar 24, 2008 5:13 pm Post subject: |
|
|
DeletedUser14087
return THandle != INVALID_HANDLE_VALUE;
That returns THandles value but not the function i would do
if(THandle != INVALID_HANDLE_VALUE){
return TRUE;
}else{
return FALSE;
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8585 Location: 127.0.0.1
|
Posted: Tue Mar 25, 2008 12:55 am Post subject: |
|
|
| blankrider wrote: | DeletedUser14087
return THandle != INVALID_HANDLE_VALUE;
That returns THandles value but not the function i would do
if(THandle != INVALID_HANDLE_VALUE){
return TRUE;
}else{
return FALSE; |
Actually, you can obtain a boolean return using that method. Example to test with:
| Code: | #include <windows.h>
#include <iostream>
bool GetExampleBoolean()
{
HANDLE hHandle = (HANDLE)1;
return (hHandle != INVALID_HANDLE_VALUE);
}
int main()
{
bool bReturn = false;
bReturn = GetExampleBoolean();
std::cout << "Value: " << bReturn;
std::cin.ignore();
std::cin.sync();
return 0;
} |
The current output would be:
Value: 1 meaning true.
hHandle does not equal INVALID_HANDLE_VALUE which is -1. Then to test the counter part, just change
HANDLE hHandle = (HANDLE)1; to:
HANDLE hHandle = INVALID_HANDLE_VALUE;
And retest, you will get Value: 0 which failed the compare since it is the same.
_________________
- Retired. |
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Mar 25, 2008 7:16 am Post subject: |
|
|
now i'm getting a new error, "Abort" :: "Retry" :: "Ignor"
| Code: | int FileAge(const char *FileName)
{
LPWIN32_FIND_DATAA FindData;
HANDLE THandle;
LPFILETIME LFT;
THandle = FindFirstFileA(PCHAR(FileName),FindData);
if(THandle != INVALID_HANDLE_VALUE)
{
FindClose(THandle);
if(FindData->dwFileAttributes && FILE_ATTRIBUTE_DIRECTORY == 0)
{
FileTimeToLocalFileTime(&FindData->ftLastWriteTime, LFT);
}
}
return -1;
}
bool FileExists(const char *FileName)
{
return FileAge(FileName) != -1;
} |
HANDLE = LongWORD
EDIT!!!
nvm i got it to work !!
| Code: | int FileExists(const char *FileName)
{
DWORD lol;
lol = GetFileAttributesA(PCHAR(FileName));
return lol != -1;
} |
i didn't knew about GetFileAttributesA API until Renko toled me, then i didn't listen to him then x0r toled so i got insist and used it
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Tue Mar 25, 2008 8:26 am Post subject: |
|
|
| DeletedUser14087 wrote: | now i'm getting a new error, "Abort" :: "Retry" :: "Ignor"
| Code: | int FileAge(const char *FileName)
{
LPWIN32_FIND_DATAA FindData;
HANDLE THandle;
LPFILETIME LFT;
THandle = FindFirstFileA(PCHAR(FileName),FindData);
if(THandle != INVALID_HANDLE_VALUE)
{
FindClose(THandle);
if(FindData->dwFileAttributes && FILE_ATTRIBUTE_DIRECTORY == 0)
{
FileTimeToLocalFileTime(&FindData->ftLastWriteTime, LFT);
}
}
return -1;
}
bool FileExists(const char *FileName)
{
return FileAge(FileName) != -1;
} |
HANDLE = LongWORD
EDIT!!!
nvm i got it to work !!
| Code: | int FileExists(const char *FileName)
{
DWORD lol;
lol = GetFileAttributesA(PCHAR(FileName));
return lol != -1;
} |
i didn't knew about GetFileAttributesA API until Renko toled me, then i didn't listen to him then x0r toled so i got insist and used it | Well, that's nice to know you always ask me for help but don't listen...
Anyway, this is neater:
| Code: | BOOL FileExists(__in LPTSTR lpszFileName)
{
return ( GetFileAttributes(lpszFileName) != 0xFFFFFFFF );
} |
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Mar 25, 2008 8:28 am Post subject: |
|
|
you're late, x0r toled me about the tchar.h library
| Code: | int FileExists(LPCTSTR lpszFileName)
{
DWORD lol;
lol = GetFileAttributes(lpszFileName);
return lol != -1;
} |
_T();
anyways, check what i've done so far with my header (i'm bored):
| Code: | #include "windows.h"
#include "stdio.h"
#include "tchar.h"
int FileExists(LPCTSTR lpszFileName)
{
DWORD lol;
lol = GetFileAttributes(lpszFileName);
return lol != -1;
}
BOOL WriteMemory(LPSTR lpClassName, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumOfBytesWritten)
{
HANDLE hProc;
HWND FW;
DWORD pID,GWTID;
FW = FindWindowA(lpClassName,NULL);
GWTID = GetWindowThreadProcessId(FW,&pID);
hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pID);
return WriteProcessMemory(hProc,lpBaseAddress,lpBuffer,nSize, lpNumOfBytesWritten);
CloseHandle(hProc);
} |
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Tue Mar 25, 2008 8:37 am Post subject: |
|
|
| DeletedUser14087 wrote: | you're late, x0r toled me about the tchar.h library
| Code: | int FileExists(LPCTSTR lpszFileName)
{
DWORD lol;
lol = GetFileAttributes(lpszFileName);
return lol != -1;
} |
_T();
anyways, check what i've done so far with my header (i'm bored):
| Code: | #include "windows.h"
#include "stdio.h"
#include "tchar.h"
int FileExists(LPCTSTR lpszFileName)
{
DWORD lol;
lol = GetFileAttributes(lpszFileName);
return lol != -1;
}
BOOL WriteMemory(LPSTR lpClassName, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumOfBytesWritten)
{
HANDLE hProc;
HWND FW;
DWORD pID,GWTID;
FW = FindWindowA(lpClassName,NULL);
GWTID = GetWindowThreadProcessId(FW,&pID);
hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pID);
return WriteProcessMemory(hProc,lpBaseAddress,lpBuffer,nSize, lpNumOfBytesWritten);
CloseHandle(hProc);
} |
| My version is still neater and more efficient. Also, your CloseHandle() is never reached since you're returning beforehand.
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Mar 25, 2008 8:51 am Post subject: |
|
|
| nice find, didn't saw that.
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Tue Mar 25, 2008 8:57 am Post subject: |
|
|
I might not be that much of a C/++ guy, but aren't there only supposed to be decl's in your header, and not actual methods?
What if Microsoft actually put all the code for the methods in Windows.h... >_>
_________________
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Mar 25, 2008 9:02 am Post subject: |
|
|
| samuri25404 wrote: | I might not be that much of a C/++ guy, but aren't there only supposed to be decl's in your header, and not actual methods?
What if Microsoft actually put all the code for the methods in Windows.h... >_> |
come again ? (i didn't understand what you mean)
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Tue Mar 25, 2008 9:08 am Post subject: |
|
|
| DeletedUser14087 wrote: | | samuri25404 wrote: | I might not be that much of a C/++ guy, but aren't there only supposed to be decl's in your header, and not actual methods?
What if Microsoft actually put all the code for the methods in Windows.h... >_> |
come again ? (i didn't understand what you mean) |
Uhh...
Ok, like you've got your method (function/procedure in Delphi), and you've got what it does.
I thought you were only supposed to declare the methods in your header, rather than actually writing everything out, then coding the whole method in your actual .cpp file.
_________________
|
|
| Back to top |
|
 |
|