|
That's is a matter of design. Says you have a MathTeacher class, each math teacher instance is suppose to hold a "multiplication table" member variable. since you are quite sure the "multiplication table" will be same everywhere (you always get 4 when you perform 2x2), it make sense to make the multiplication table as static const scope.
so, instead of having 10000 copies of MathTeacher objects (hence, 10000 copies of "multiplication table" since each teacher is having one copy of table). by having static scope member variable, we can have 10000 copies of teachers, share one copy of math table, which we are sure it will be always constant and never changed.
hope this help.
|
|
|
|
|
I agree with moonen. Everyone looks daft in a new area in experts eyes.
The solution to the problem is to init the map in a constructor:
class CMy<br />
{<br />
public:<br />
CMy();<br />
private:<br />
static std::map<int, int> mymap;<br />
static CMy *p;<br />
};<br />
<br />
CMy* CMy::p = 0;<br />
CMy::CMy()<br />
{<br />
mymap[1] = 1;<br />
}
|
|
|
|
|
However, that need to instance the object first before the static variable can get initialized.
I have a suggestion of doing so by using lazy initialization. not so good since it is not really insert the element in static scope. however, this at least still meet my initial need: get element inserted into stl map before the class object instance created.
class x {
public:
static const map<int, int="">& GetMap {
static const map<int, int=""> m;
if(m.size() == 0) {
// Perform element insertion here.
//
}
return m;
}
};
|
|
|
|
|
never mind, I got stupid. I shouldn't post in the morning.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
-- modified at 9:59 Friday 23rd September, 2005
|
|
|
|
|
i guess u need a 2d array,
declare char author_name[10][30];
[10] if u want multiple names to be stored.
[30] size of the names
if u want to store a single name use char auth_name[10] or char *auth_name;
V
|
|
|
|
|
u can also use char* author_name[10].only difference is that it will have addresses of multiple names.it is better than char author_name[10][30] becuse every multiple name may not be of length of 30
|
|
|
|
|
sunit5 wrote:
u can also use char* author_name[10].only difference is that it will have addresses of multiple names.it is better than char author_name[10][30] becuse every multiple name may not be of length of 30
Right .... This what be call better memory Management....
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
hi,
u need to write like this
cin>>author_name
in previous case it is declared as a pointer and the name of the array wil give u the addres location so this will work (author_name),if you go this way it will give you char ** error
check it out and tell me out .
farpointer.
|
|
|
|
|
Hi!
Now I wanna sent 4 bytes data by calling DeviceIoControl() from my directshow application to my streaming minidriver.First,I call the CreateFile() to get a device handle,and the minidriver can receive a IRP_MJ_CREATE IRP,and the CreateFile() returns nonzero.But when the app runs to DeviceIoControl(),the minidriver does not recieve any SRB.The DeviveIoControl() returns zero,and GetlastError() returns 50,which says:"The request is not supposed."
I difined my own IOCTL command in my driver and app.
What causes the error 50 or if there is any other way to send data to steaming minidriver from directshow application?
Thanks for any help!
momer
|
|
|
|
|
Hello:
I am having following error while using CryptEncrypt and CryptDecrypt for public private key pair of Windows CryptoApi .I am using windowsXP as OS.
The code of the class I am using is given below. I am having problem while using Encrypt and Decrypt functions of the class. Can anybody would tell me where is the error and how to remove it or could make the code right where error occurs.
Thanks,
Naeem
//////////////////////////////Header File/////////////////////////
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <wincrypt.h>
#include <tchar.h>
// Helper class definition to generate and export Public/Private keys
// for Asymmetric encryption. The semantics for usage are:
// Call AcquireContext with a container name, call
// GenerateKeyPair next and then call ExportPublicKey or
// ExportPrivateKey.
class CryptoHelper
{
private:
HCRYPTPROV m_hCryptProv;
HCRYPTKEY m_hSessionKey;
BYTE *m_pPublicKey;
BYTE *m_pPrivateKey;
BYTE *m_pEncSessionKey;
DWORD dwPrivateKeyLen;
DWORD dwPublicKeyLen;
DWORD dwEncSessionKeyLen;
public:
HCRYPTKEY m_hCryptKey;
CryptoHelper();
~CryptoHelper();
HRESULT AcquireContext(LPCTSTR wszContainerName);
HRESULT GenerateKeyPair();
HRESULT ExportPublicKey(BYTE **ppPublicKey, DWORD &cbKeySize);;
HRESULT ExportPrivateKey(BYTE **ppPrivateKey, DWORD &cbKeySize);
void Encrypt(char **Input,char **Output,DWORD &dwLen);
void Decrypt(char **Input,char **Output,DWORD &dwLen);
};
//////////////////////////////The CPP File/////////////////////////
#include "CryptoHelper.h"
#include <stdio.h>
//#include "Cspdk.h"
// The RSA public-key key exchange algorithm
#define ENCRYPT_ALGORITHM CALG_RSA_KEYX
// The high order WORD 0x0200 (decimal 512)
// determines the key length in bits.
#define KEYLENGTH 0x02000000
#define SESS_KEYLENGTH 0x00800000 //128 Bit
//--------------------------------------------------------------------
// The constructor initializes the member variables
// to NULL.
//--------------------------------------------------------------------
CryptoHelper::CryptoHelper()
{
m_hCryptProv = NULL;
m_hCryptKey = NULL;
m_hSessionKey = NULL;
m_pPublicKey=NULL;
m_pPrivateKey=NULL;
m_pEncSessionKey=NULL;
dwPrivateKeyLen=0;
dwPublicKeyLen=0;
dwEncSessionKeyLen=0;
}
//--------------------------------------------------------------------
// The destructor releases the handles acquired
// when an object goes out of scope.
//--------------------------------------------------------------------
CryptoHelper::~CryptoHelper()
{
if (m_hCryptProv)
{
CryptReleaseContext(m_hCryptProv,0);
m_hCryptProv = NULL;
}
if (m_hCryptKey)
m_hCryptKey = NULL;
}
//--------------------------------------------------------------------
// This method calls the CryptAcquireContext function
// to get a handle to a key container owned by the the
// Microsoft Enhanced Cryptographic Provider.
//--------------------------------------------------------------------
HRESULT CryptoHelper::AcquireContext(LPCTSTR wszContainerName)
{
HRESULT hr = S_OK;
DWORD dwErrCode;
// Release a previously acquired handle to the key container.
if (m_hCryptProv != NULL)
{
CryptReleaseContext(m_hCryptProv,0);
m_hCryptProv = NULL;
}
// Release a previously acquired handle to key-pair.
if (m_hCryptKey != NULL)
m_hCryptKey = NULL;
// Attempt to acquire a context and a key container.
// The context will use Microsoft Enhanced Cryptographic
// Provider for the RSA_FULL provider type.
if(!CryptAcquireContext(&m_hCryptProv,
/*NULL*/wszContainerName,
/*NULL*/MS_ENHANCED_PROV,
PROV_RSA_FULL,
CRYPT_MACHINE_KEYSET))
{
// An error occurred in acquiring the context. This could mean
// that the key container requested does not exist. In this case,
// the function can be called again to attempt to create a new key
// container.
if (GetLastError() == NTE_BAD_KEYSET)
{
if(!CryptAcquireContext(&m_hCryptProv,
/*NULL*/wszContainerName,
/*NULL*/MS_ENHANCED_PROV,
PROV_RSA_FULL,
CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET))
{
dwErrCode = GetLastError();
return HRESULT_FROM_WIN32(dwErrCode);
}
}
else
{
dwErrCode = GetLastError();
return HRESULT_FROM_WIN32(dwErrCode);
}
}
return hr;
}
//--------------------------------------------------------------------
// This method calls the CryptGenKey function to get a handle to an
// exportable key-pair. The key-pair is generated with the RSA public-key
// key exchange algorithm using Microsoft Enhanced Cryptographic Provider.
//--------------------------------------------------------------------
HRESULT CryptoHelper::GenerateKeyPair()
{
HRESULT hr = S_OK;
DWORD dwErrCode;
// If the handle to key container is NULL, fail.
if (m_hCryptProv == NULL)
return E_FAIL;
// Release a previously acquired handle to key-pair.
if (m_hCryptKey)
m_hCryptKey = NULL;
// Call the CryptGenKey method to get a handle
// to a new exportable key-pair.
if(!CryptGenKey(m_hCryptProv,
ENCRYPT_ALGORITHM,
KEYLENGTH | CRYPT_EXPORTABLE,
&m_hCryptKey))
{
dwErrCode = GetLastError();
return HRESULT_FROM_WIN32(dwErrCode);
}
return hr;
}
//--------------------------------------------------------------------
// This method calls the CryptExportKey function to get the Public key
// in a byte array. The byte array is allocated on the heap and the size
// of this is returned to the caller. The caller is responsible for releasing // this memory using a delete call.
//--------------------------------------------------------------------
HRESULT CryptoHelper::ExportPublicKey(BYTE **ppPublicKey, DWORD &cbKeySize)
{
HRESULT hr = S_OK;
DWORD dwErrCode;
DWORD dwBlobLen;
BYTE *pbKeyBlob = NULL;
// If the handle to key container is NULL, fail.
if (m_hCryptKey == NULL)
return E_FAIL;
// This call here determines the length of the key
// blob.
if(!CryptExportKey(
m_hCryptKey,
NULL,
PUBLICKEYBLOB,
0,
NULL,
&dwBlobLen))
{
dwErrCode = GetLastError();
return HRESULT_FROM_WIN32(dwErrCode);
}
// Allocate memory for the pbKeyBlob.
if((pbKeyBlob = new BYTE[dwBlobLen]) == NULL)
{
return E_OUTOFMEMORY;
}
// Do the actual exporting into the key BLOB.
if(!CryptExportKey(
m_hCryptKey,
NULL,
PUBLICKEYBLOB,
0,
pbKeyBlob,
&dwBlobLen))
{
delete pbKeyBlob;
dwErrCode = GetLastError();
return HRESULT_FROM_WIN32(dwErrCode);
}
else
{
*ppPublicKey = pbKeyBlob;
m_pPublicKey =pbKeyBlob;
cbKeySize = dwBlobLen;
dwPublicKeyLen=dwBlobLen;
}
return hr;
}
//--------------------------------------------------------------------
// This method calls the CryptExportKey function to get the Private key
// in a byte array. The byte array is allocated on the heap and the size
// of this is returned to the caller. The caller is responsible for releasing // this memory using a delete call.
//--------------------------------------------------------------------
HRESULT CryptoHelper::ExportPrivateKey(BYTE **ppPrivateKey, DWORD &cbKeySize)
{
HRESULT hr = S_OK;
DWORD dwErrCode;
DWORD dwBlobLen;
BYTE *pbKeyBlob;
// If the handle to key container is NULL, fail.
if (m_hCryptKey == NULL)
return E_FAIL;
// This call here determines the length of the key
// blob.
if(!CryptExportKey(
m_hCryptKey,
NULL,
PRIVATEKEYBLOB,
0,
NULL,
&dwBlobLen))
{
dwErrCode = GetLastError();
return HRESULT_FROM_WIN32(dwErrCode);
}
// Allocate memory for the pbKeyBlob.
if((pbKeyBlob = new BYTE[dwBlobLen]) == NULL)
{
return E_OUTOFMEMORY;
}
// Do the actual exporting into the key BLOB.
if(!CryptExportKey(
m_hCryptKey,
NULL,
PRIVATEKEYBLOB,
0,
pbKeyBlob,
&dwBlobLen))
{
delete pbKeyBlob;
dwErrCode = GetLastError();
return HRESULT_FROM_WIN32(dwErrCode);
}
else
{
*ppPrivateKey = pbKeyBlob;
m_pPrivateKey=pbKeyBlob;
cbKeySize = dwBlobLen;
dwPrivateKeyLen=dwBlobLen;
}
return hr;
}
////////////////////////the function with problem//////////////////
void CryptoHelper::Encrypt(char **Input,char **Output,DWORD &dwLen)
{//dwLen in start gives input string length on return it gives output string
//length
HCRYPTKEY hPubKey=NULL;
HCRYPTHASH hHash=NULL;
DWORD dwBufLen;
//importing the public key . In real situation this would be the //public key of other user a
if(CryptImportKey(m_hCryptProv,m_pPublicKey,dwPublicKeyLen,0,CRYPT_EXPORTABLE,
&hPubKey))
{
MessageBox(NULL,"public key achieved","",MB_OK);
}
if(CryptEncrypt(hPubKey,0,TRUE,0,NULL,&dwBufLen,dwLen))//get //length to hold buffer but this fails
{
dwBufLen=dwLen;
*Output=new char[dwLen+1];
memset(*Output,0,dwLen+1);
memcpy(*Output,*Input,dwLen);
if(!CryptEncrypt(hPubKey,0,TRUE,0,(BYTE*)*Output,&dwBufLen,dwLen))
{
::MessageBox(NULL,"Fail","",MB_OK);
dwLen=0;
}
else
dwLen=dwBufLen;
}
}
void CryptoHelper::Decrypt(char **Input,char **Output,DWORD &dwLen)
{
//now first get the session key from the Encrypted Session key
HCRYPTKEY hSessionKey=NULL,hPrvtKey;
DWORD dwBufLen;
if(CryptImportKey(m_hCryptProv,m_pPrivateKey,dwPrivateKeyLen,0,CRYPT_EXPORTABLE,
&hPrvtKey))
{
MessageBox(NULL,"private key achieved","",MB_OK);
}
if(CryptDecrypt(hPrvtKey,0,TRUE,0,NULL,&dwBufLen))//get //length to hold buffer but this fails
{
*Output=new char[dwBufLen+1];
memset(*Output,0,dwBufLen+1);
memcpy(*Output,*Input,dwLen);
if(!CryptDecrypt(hPrvtKey,0,TRUE,0,(BYTE*)*Output,&dwBufLen))
{
::MessageBox(NULL,"Fail","",MB_OK);
dwLen=0;
}
else
dwLen=dwBufLen;
}
}
//////////////////////////////The Main File/////////////////////////
#include <stdio.h>
#include "CryptoHelper.h"
void main()
{
CryptoHelper cryptoHlpr;
BYTE *pbPublicKey = NULL, *pbPrivateKey = NULL;
DWORD dwPublicKeySize = 0,dwPrivateKeySize = 0;
HRESULT hr = S_OK;
// Get the key container context.
if (FAILED(hr = cryptoHlpr.AcquireContext(("TestContainer"))))
{
// Call FormatMessage to display the error returned in hr.
return;
}
// Generate the public/private key pair.
if (FAILED(hr = cryptoHlpr.GenerateKeyPair()))
{
// Call FormatMessage to display the error returned in hr.
return;
}
// Export out the public key blob.
cryptoHlpr.ExportPublicKey(&pbPublicKey, dwPublicKeySize);
// Export out the private key blob.
cryptoHlpr.ExportPrivateKey(&pbPrivateKey, dwPrivateKeySize);
//now encrypting the text
char *input="abcdef123456";
char *output,*Result;
DWORD dwLen=strlen(input);
cryptoHlpr.Encrypt(&input,&output,dwLen);//error
//now decrypting the text
cryptoHlpr.Decrypt(&output,&Result,dwLen);//error
if (pbPublicKey)
delete [] pbPublicKey;
// Delete the private key blob allocated by the
// ExportPrivateKey method.
if (pbPrivateKey)
delete [] pbPrivateKey;
return;
}
|
|
|
|
|
And why don't you use the button "Code Block" when posting code ?
Your posting is completely unreadable.
|
|
|
|
|
write a C function that produces a histogram of the number of occurrence of each letter in a character string. your function should be case independent. your function should have two parameters : the first is a pointer to an array of 26 int that holds the histogram; the second is a pointer to a character string. function return the number of letters found in the character string as an integer
|
|
|
|
|
int DestroyedFuture(int* array, char* str) <br />
{<br />
int i;<br />
int j;<br />
int len;<br />
int length;<br />
len = strlen(str);<br />
char c;<br />
length = 0;<br />
for (i = 0; i < len; i++)<br />
{<br />
c = str[i];<br />
c = toupper(c);<br />
for (j = 65; j < 65+26; j++)<br />
{<br />
if (c == j)<br />
{<br />
length++;<br />
array[j-65]++;<br />
}<br />
}<br />
}<br />
return length; <br />
}
It took about 2 minutes to write this code.
I will get flamed for it I know. Heh. I have nothing better to do.
this is this.
|
|
|
|
|
Nice job but that inner for loop is really unnecessary. How about:
int DestroyedFuture( int *array, const char *str )
{
int count = 0,
len = strlen(str);
for (int i = 0; i < len; i++)
{
if (isalpha(str[i]))
{
char c = toupper(str[i]);
array[c - 65]++;
count++;
}
}
return count;
}
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Your subject indicates you need help. What have you done that you need help with? If nothing, you might want to consider changing the subject to "i need you to fix the histogram.plz" instead.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
hi,
My Application had Two child Dialogs, i want these Dialogs should be placed on taskbar when they are minimized...
plz help me
-- modified at 3:01 Friday 23rd September, 2005
|
|
|
|
|
While creating dialog bar, make desktop window as parent , either by using constructor or while creating.
You can get desktop window handle by using GetDesktopWindow()
|
|
|
|
|
When I send a data from one PC to another PC, I want it automatic send back a respond data. I don't know whether got this kind of fuction or not. Can someone tell me...
|
|
|
|
|
jyhhaur wrote:
When I send a data from one PC to another PC
How do you send the data ?? Is it with serial port, through a LAN, a wireless connection or even little animals that bring the data to the computer (if you don't explain, we will need to use our imagination and I have a lot of imagination )
jyhhaur wrote:
I want it automatic send back a respond data
What is 'automatic' ? The computer, without any program running on it should send your little animals back (of course, in case if this was the communication media ).
Sorry, but if you don't provide us with information, we cannot answer your question.
|
|
|
|
|
I send the data through TCP. the 'automatic' is I use a program that I have to send a data through TCP and I want it respond to my data. I just think whether it have something like that or not.....or a function that can write so that it will respond to my data.
|
|
|
|
|
u mean the receiver after getting the data should say, "i got it"??that u've to code urself..;P
V
|
|
|
|
|
Does any one has got the idea where i could find the file dlibtiff.I 'm working on project in which i need to convert tiff to PDF for this I am using
libtiff.lib but when i try to compile i get this error that could not find dlibtiff.lib
No where on NET i v found this
Your help is required...
asifrogers
|
|
|
|
|
|
If I define int a=2; inside a function body which section of the executable file format will contain this information ?
3.what is the vc++ executable file format used on microsoft windows os?
Nice talking to you.
|
|
|
|
|
|