|
Hi,
Thanks a lot.
But still I have a problem.I got the ODBC driver and created a DSN and wrote a sample WIN32 Console appln to test it .But it is not working.Please check this code.
#include "stdafx.h"
#include<afx.h>
#include<afxdb.h>
int main(int argc, char* argv[])
{
printf("Hello World!\n");
CDatabase c;
c.Open(_T("test"),FALSE,FALSE,_T("ODBC"),TRUE);
return 0;
}
It is giving the following errors.
--------------------Configuration: kardb - Win32 Debug--------------------
Compiling...
kardb.cpp
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/kardb.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
kardb.exe - 3 error(s), 0 warning(s)
|
|
|
|
|
Hello guys,
do you know if it is possible to get a file name from a file handle?
I tried the code below and strFileName its empty. I've tried to find some info into the MSDN and I cannot manage to find out a method that take in input the file handle and gives me back the file name.
Thanx,
Fabio
CFile* file = new CFile(_T("C:\\test.txt"), CFile::modeCreate|CFile::modeWrite|CFile::shareDenyNone);
CFile newFile((int) file->m_hFile);
newFile.Write("C", 1);
strFileName = newFile.GetFilePath();
file->Close();
|
|
|
|
|
i know that there is a way in kernel mode... but i don't know if it is exported
to user mode.
Don't try it, just do it!
|
|
|
|
|
Highly coupled code is code where the dependencies between things are dense, lots of things depend on other things.
On Unix i know makedepend can give u the dependecies of your classes.
Do any of you have experience in such tool under windows? (better to be opensource)
Thanks and regards,
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
I do not know if this helps, but Visual C++ comes with Depends that will tell you what you application depends on. There is also an add on that you can get (may be open source) called BeSweet. You can also use Doxygen to genterate documentation for your project, I beleive it will show all the dependencies in your project (read the article on Doxygen at codeproject).
INTP
|
|
|
|
|
Below are the code for encrypt and decrypt a text file by supplying a source file, destination file and password.
The problem is if I encrypt the file in Win2000 and decrypt it in WinXP the result is junk and vise versa. Win2000 to Win2000 or WinXP to WinXP work ok. Can anyone tell me what the problem is? Thanks alot.
Encrypt.c
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wincrypt.h>
#ifdef USE_BLOCK_CIPHER
// defines for RC2 block cipher
#define ENCRYPT_ALGORITHM CALG_RC2
#define ENCRYPT_BLOCK_SIZE 8
#else
// defines for RC4 stream cipher
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 1
#endif
static BOOL CAPIEncryptFile(PCHAR szSource, PCHAR szDestination, PCHAR szPassword);
/ **************************************************
***************************/
static BOOL CAPIEncryptFile(PCHAR szSource, PCHAR szDestination, PCHAR szPassword)
{
FILE *hSource = NULL;
FILE *hDestination = NULL;
INT eof = 0;
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTKEY hXchgKey = 0;
HCRYPTHASH hHash = 0;
PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;
PBYTE pbBuffer = NULL;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
BOOL status = FALSE;
// Open source file.
if((hSource = fopen(szSource,"rb")) == NULL) {
printf("Error opening Plaintext file!\n");
goto done;
}
// Open destination file.
if((hDestination = fopen(szDestination,"wb")) == NULL) {
printf("Error opening Ciphertext file!\n");
goto done;
}
// Get handle to the default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
printf("Error %x during CryptAcquireContext!\n", GetLastError());
goto done;
}
if(szPassword == NULL) {
// Encrypt the file with a random session key.
// Create a random session key.
if(!CryptGenKey(hProv, ENCRYPT_ALGORITHM, CRYPT_EXPORTABLE, &hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
goto done;
}
// Get handle to key exchange public key.
if(!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hXchgKey)) {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
goto done;
}
// Determine size of the key blob and allocate memory.
if(!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, NULL, &dwKeyBlobLen)) {
printf("Error %x computing blob length!\n", GetLastError());
goto done;
}
if((pbKeyBlob = malloc(dwKeyBlobLen)) == NULL) {
printf("Out of memory!\n");
goto done;
}
// Export session key into a simple key blob.
if(!CryptExportKey(hKey, hXchgKey, SIMPLEBLOB, 0, pbKeyBlob, &dwKeyBlobLen)) {
printf("Error %x during CryptExportKey!\n", GetLastError());
goto done;
}
// Release key exchange key handle.
CryptDestroyKey(hXchgKey);
hXchgKey = 0;
// Write size of key blob to destination file.
fwrite(&dwKeyBlobLen, sizeof(DWORD), 1, hDestination);
if(ferror(hDestination)) {
printf("Error writing header!\n");
goto done;
}
// Write key blob to destination file.
fwrite(pbKeyBlob, 1, dwKeyBlobLen, hDestination);
if(ferror(hDestination)) {
printf("Error writing header!\n");
goto done;
}
} else {
// Encrypt the file with a session key derived from a password.
// Create a hash object.
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
printf("Error %x during CryptCreateHash!\n", GetLastError());
goto done;
}
// Hash in the password data.
if(!CryptHashData(hHash, szPassword, strlen(szPassword), 0)) {
printf("Error %x during CryptHashData!\n", GetLastError());
goto done;
}
// Derive a session key from the hash object.
if(!CryptDeriveKey(hProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey)) {
printf("Error %x during CryptDeriveKey!\n", GetLastError());
goto done;
}
// Destroy the hash object.
CryptDestroyHash(hHash);
hHash = 0;
}
// Determine number of bytes to encrypt at a time. This must be a multiple
// of ENCRYPT_BLOCK_SIZE.
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
// Determine the block size. If a block cipher is used this must have
// room for an extra block.
if(ENCRYPT_BLOCK_SIZE > 1) {
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
} else {
dwBufferLen = dwBlockLen;
}
// Allocate memory.
if((pbBuffer = malloc(dwBufferLen)) == NULL) {
printf("Out of memory!\n");
goto done;
}
// Encrypt source file and write to Source file.
do {
// Read up to 'dwBlockLen' bytes from source file.
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource)) {
printf("Error reading Plaintext!\n");
goto done;
}
eof = feof(hSource);
// Encrypt data
if(!CryptEncrypt(hKey, 0, eof, 0, pbBuffer, &dwCount, dwBufferLen)) {
printf("bytes required:%d\n",dwCount);
printf("Error %x during CryptEncrypt!\n", GetLastError());
goto done;
}
// Write data to destination file.
fwrite(pbBuffer, 1, dwCount, hDestination);
if(ferror(hDestination)) {
printf("Error writing Ciphertext!\n");
goto done;
}
} while(!feof(hSource));
status = TRUE;
printf("OK\n");
done:
// Close files.
if(hSource) fclose(hSource);
if(hDestination) fclose(hDestination);
// Free memory.
if(pbKeyBlob) free(pbKeyBlob);
if(pbBuffer) free(pbBuffer);
// Destroy session key.
if(hKey) CryptDestroyKey(hKey);
// Release key exchange key handle.
if(hXchgKey) CryptDestroyKey(hXchgKey);
// Destroy hash object.
if(hHash) CryptDestroyHash(hHash);
// Release provider handle.
if(hProv) CryptReleaseContext(hProv, 0);
return(status);
}
/ **************************************************
***************************/
void init(void)
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
CHAR szUserName[100];
DWORD dwUserNameLen = 100;
// Attempt to acquire a handle to the default key container.
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) {
// Some sort of error occured.
// Create default key container.
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
printf("Error creating key container!\n");
exit(1);
}
// Get name of default key container.
if(!CryptGetProvParam(hProv, PP_CONTAINER, szUserName, &dwUserNameLen, 0)) {
// Error getting key container name.
szUserName[0] = 0;
}
printf("Create key container '%s'\n",szUserName);
}
// Attempt to get handle to signature key.
if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey)) {
if(GetLastError() == NTE_NO_KEY) {
// Create signature key pair.
printf("Create signature key pair\n");
if(!CryptGenKey(hProv,AT_SIGNATURE,0,&hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
exit(1);
} else {
CryptDestroyKey(hKey);
}
} else {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
exit(1);
}
}
// Attempt to get handle to exchange key.
if(!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hKey)) {
if(GetLastError()==NTE_NO_KEY) {
// Create key exchange key pair.
printf("Create key exchange key pair\n");
if(!CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
exit(1);
} else {
CryptDestroyKey(hKey);
}
} else {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
exit(1);
}
}
CryptReleaseContext(hProv,0);
// printf("OK\n");
}
/ **************************************************
***************************/
void _cdecl main(int argc, char *argv[])
{
PCHAR szSource = NULL;
PCHAR szDestination = NULL;
PCHAR szPassword = NULL;
init();
// Validate argument count.
if(argc != 3 && argc != 4) {
printf("USAGE: encrypt <dest file=""> [ <password> ]\n");
exit(1);
}
// Parse arguments.
szSource = argv[1];
szDestination = argv[2];
if(argc == 4) {
szPassword = argv[3];
}
if(!CAPIEncryptFile(szSource, szDestination, szPassword)) {
printf("Error encrypting file!\n");
exit(1);
}
exit(0);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Decrypt.c
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wincrypt.h>
#ifdef USE_BLOCK_CIPHER
// defines for RC2 block cipher
#define ENCRYPT_ALGORITHM CALG_RC2
#define ENCRYPT_BLOCK_SIZE 8
#else
// defines for RC4 stream cipher
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 1
#endif
static BOOL CAPIDecryptFile(PCHAR szSource, PCHAR szDestination, PCHAR szPassword);
/ **************************************************
***************************/
static BOOL CAPIDecryptFile(PCHAR szSource, PCHAR szDestination, PCHAR szPassword)
{
FILE *hSource = NULL;
FILE *hDestination = NULL;
INT eof = 0;
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTHASH hHash = 0;
PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;
PBYTE pbBuffer = NULL;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
BOOL status = FALSE;
// Open source file.
if((hSource = fopen(szSource,"rb")) == NULL) {
printf("Error opening Ciphertext file!\n");
goto done;
}
// Open destination file.
if((hDestination = fopen(szDestination,"wb")) == NULL) {
printf("Error opening Plaintext file!\n");
goto done;
}
// Get handle to the default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
printf("Error %x during CryptAcquireContext!\n", GetLastError());
goto done;
}
if(szPassword == NULL) {
// Decrypt the file with the saved session key.
// Read key blob length from source file and allocate memory.
fread(&dwKeyBlobLen, sizeof(DWORD), 1, hSource);
if(ferror(hSource) || feof(hSource)) {
printf("Error reading file header!\n");
goto done;
}
if((pbKeyBlob = malloc(dwKeyBlobLen)) == NULL) {
printf("Out of memory or improperly formatted source file!\n");
goto done;
}
// Read key blob from source file.
fread(pbKeyBlob, 1, dwKeyBlobLen, hSource);
if(ferror(hSource) || feof(hSource)) {
printf("Error reading file header!\n");
goto done;
}
// Import key blob into CSP.
if(!CryptImportKey(hProv, pbKeyBlob, dwKeyBlobLen, 0, 0, &hKey)) {
printf("Error %x during CryptImportKey!\n", GetLastError());
goto done;
}
} else {
// Decrypt the file with a session key derived from a password.
// Create a hash object.
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
printf("Error %x during CryptCreateHash!\n", GetLastError());
goto done;
}
// Hash in the password data.
if(!CryptHashData(hHash, szPassword, strlen(szPassword), 0)) {
printf("Error %x during CryptHashData!\n", GetLastError());
goto done;
}
// Derive a session key from the hash object.
if(!CryptDeriveKey(hProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey)) {
printf("Error %x during CryptDeriveKey!\n", GetLastError());
goto done;
}
// Destroy the hash object.
CryptDestroyHash(hHash);
hHash = 0;
}
// Determine number of bytes to decrypt at a time. This must be a multiple
// of ENCRYPT_BLOCK_SIZE.
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
dwBufferLen = dwBlockLen;
// Allocate memory.
if((pbBuffer = malloc(dwBufferLen)) == NULL) {
printf("Out of memory!\n");
goto done;
}
// Decrypt source file and write to destination file.
do {
// Read up to 'dwBlockLen' bytes from source file.
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource)) {
printf("Error reading Ciphertext!\n");
goto done;
}
eof = feof(hSource);
// Decrypt data
if(!CryptDecrypt(hKey, 0, eof, 0, pbBuffer, &dwCount)) {
printf("Error %x during CryptDecrypt!\n", GetLastError());
goto done;
}
// Write data to destination file.
fwrite(pbBuffer, 1, dwCount, hDestination);
if(ferror(hDestination)) {
printf("Error writing Plaintext!\n");
goto done;
}
} while(!feof(hSource));
status = TRUE;
printf("OK\n");
done:
// Close files.
if(hSource) fclose(hSource);
if(hDestination) fclose(hDestination);
// Free memory.
if(pbKeyBlob) free(pbKeyBlob);
if(pbBuffer) free(pbBuffer);
// Destroy session key.
if(hKey) CryptDestroyKey(hKey);
// Destroy hash object.
if(hHash) CryptDestroyHash(hHash);
// Release provider handle.
if(hProv) CryptReleaseContext(hProv, 0);
return(status);
}
/ **************************************************
***************************/
void init(void)
{
HCRYPTPROV hProv;
HCRYPTKEY hKey;
CHAR szUserName[100];
DWORD dwUserNameLen = 100;
// Attempt to acquire a handle to the default key container.
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) {
// Some sort of error occured.
// Create default key container.
if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
printf("Error creating key container!\n");
exit(1);
}
// Get name of default key container.
if(!CryptGetProvParam(hProv, PP_CONTAINER, szUserName, &dwUserNameLen, 0)) {
// Error getting key container name.
szUserName[0] = 0;
}
printf("Create key container '%s'\n",szUserName);
}
// Attempt to get handle to signature key.
if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey)) {
if(GetLastError() == NTE_NO_KEY) {
// Create signature key pair.
printf("Create signature key pair\n");
if(!CryptGenKey(hProv,AT_SIGNATURE,0,&hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
exit(1);
} else {
CryptDestroyKey(hKey);
}
} else {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
exit(1);
}
}
// Attempt to get handle to exchange key.
if(!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hKey)) {
if(GetLastError()==NTE_NO_KEY) {
// Create key exchange key pair.
printf("Create key exchange key pair\n");
if(!CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hKey)) {
printf("Error %x during CryptGenKey!\n", GetLastError());
exit(1);
} else {
CryptDestroyKey(hKey);
}
} else {
printf("Error %x during CryptGetUserKey!\n", GetLastError());
exit(1);
}
}
CryptReleaseContext(hProv,0);
// printf("OK\n");
}
/ **************************************************
***************************/
void _cdecl main(int argc, char *argv[])
{
PCHAR szSource = NULL;
PCHAR szDestination = NULL;
PCHAR szPassword = NULL;
init();
// Validate argument count.
if(argc != 3 && argc != 4) {
printf("USAGE: decrypt <dest file=""> [ <password> ]\n");
exit(1);
}
// Parse arguments.
szSource = argv[1];
szDestination = argv[2];
if(argc == 4) {
szPassword = argv[3];
}
if(!CAPIDecryptFile(szSource, szDestination, szPassword)) {
printf("Error encrypting file!\n");
exit(1);
}
exit(0);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
All of a sudden, Visual Studio started to give me the message "One or more files are out of date or do not exist" and list all my obj files (and bsc and exe too), everytime I click Execute Program (Ctrl-F5) or Go (F5). It's ok if the files are really outdated, but the problem occurs after I Rebuild All and then Execute. And it compiles and build all the sources over and over. It is happening only with the project I'm working on. I tried others and it works fine. Any suggestions ?
Thanks
Geraldo
|
|
|
|
|
Check the date of files in sourcesafe, I once changed my system date and then changed it back, and as a result I had that problem until the date went past the date I ran at for a day.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
I'm not using sourcesafe. I solved the problem this way: I created a new project, created every cpp and h file empty (it must have been 12 files) and then copied and pasted the source files one by one and after. It took me all day long, but now I have a working copy again. Very strange. I kept the old copy to play with when I got time. I hope it won't happen again.
Thanks.
|
|
|
|
|
someone changed ur system date to a forward date and then you edited the files (and changed the date back) and saved it happens to me all the time , just edit the files once more and their modifed date will be taken care of (after changing the time)
Live as if your were to die tomorrow. Learn as if you were to live forever.
-Mahatma Gandhi
|
|
|
|
|
Hi,
How can I get the IP address of the system in which the executable is running using C functions(both for linux and windows).
Karteek
|
|
|
|
|
gethostbyname() and so on
|
|
|
|
|
Anonymous wrote:
gethostbyname()...
Preceded by gethostname() .
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Hi,
Can you give an example?I tried it but it did not work.
Thank you
Karteek
|
|
|
|
|
http://tinyurl.com/3df5s[^]
char szHost[128];
gethostname(szHost, sizeof(szHost));
struct hostent *pHost;
pHost = gethostbyname(szHost);
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Hi,
I did the same thing and I tried to print pHost->h_addr_list[0].But I am not getting the ip address of the system
Karteek
|
|
|
|
|
karteek wrote:
I am not getting the ip address of the system
Sure you are. It's just not in dotted notation like you are accustomed to seeing it. You need to copy pHost->h_addr_list[0] into a sockaddr_in structure, and then call inet_ntoa() passing it the sin_addr member of that structure.
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Hello Guys,
How ru all doing..i need some some help in VC++, When iam declaring an CInternetSession variable as global, i get some debug assertion error, when the application runs, i have created the applicaiton through MFC Wizard, in the dialog i do some operations for FTp Connection, so i establish a CInternetSession and CFTPConnection Variable globally, because it is accesed by many functions, when the app runs, the error is thrown off, when i declare the CInternetSession inside a function, i dont get any error, but i want it only globally.
|
|
|
|
|
I just began learning assembler programming on the Linux/i386. I know how the shell code works and can write the assembler for it. I also understand what the code in the main() function does. Creates an int pointer, sets it equal to the cast address of the pointer + 2 then copies the cast shell code int to the dereference of the int pointer and somehow executes. If you have the time could you please explain how the code in the main() function works. Thanks, I really appreciate it.
#include <stdio.h>
char shellcode[] =
"\x31\xc0\x50\x68\x31\x33\x33\x37"
"\x66\xb9\xed\x01\x89\xe3\xb0\x27"
"\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
"\x80\x90\x90\x90\x55\x89";
int main()
{
int * ret;
ret = (int *)&ret + 2;
*ret = (int)shellcode;
}
John
Programmer
|
|
|
|
|
That's a classical buffer overrun. You're basically modifying the function return address to that of shellcode . When the function tries to return using the return address provided by the C-runtime which called main, it'll return straight into the memory area of shellcode . shellcode contains CPU instructions which will do something which was not intended (well, in this case it is intended, but it's just for show).
Here is a more detailed document on the subject: http://www.cs.ucsb.edu/~jzhou/security/overflow.html[^]
I just hope you're not a bad guy
--
In the land of the blind, be king![^]
|
|
|
|
|
|
|
The 22-Nov entry in this blog[^] may provide an answer.
Btw, I hate to do this to you, but my ISP just upped my standard broadband connection (downloads) to 3Mb/sec (about 400 KB/sec).
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
|
OK, you got me where it hurts. You know I live on ham and cheese sandwiches, while you casually gorge yourself on wondrous delights that for me are but a distant memory. I'd kill for a medhu wada soaked in spicy sambar...
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|