Click here to Skip to main content
16,006,341 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHTTP Cookies ? Pin
Putta_V22-Nov-05 19:05
Putta_V22-Nov-05 19:05 
QuestionJump to a location in floppy Pin
Aqueel A. Mirza22-Nov-05 19:04
Aqueel A. Mirza22-Nov-05 19:04 
AnswerRe: Jump to a location in floppy Pin
kakan22-Nov-05 19:35
professionalkakan22-Nov-05 19:35 
AnswerRe: Jump to a location in floppy Pin
Bob Stanneveld23-Nov-05 6:36
Bob Stanneveld23-Nov-05 6:36 
Questionmindow message of popup menu. Pin
includeh1022-Nov-05 19:02
includeh1022-Nov-05 19:02 
AnswerRe: mindow message of popup menu. Pin
Marc Soleda22-Nov-05 20:35
Marc Soleda22-Nov-05 20:35 
AnswerRe: mindow message of popup menu. Pin
Owner drawn22-Nov-05 22:03
Owner drawn22-Nov-05 22:03 
QuestionMemory Handling Check Pin
Anzy22-Nov-05 18:53
Anzy22-Nov-05 18:53 
Hi..

I'm very new to C++ and have just written 2 methods that are pretty unstable (ie. same input may or may not run correctly). My gut feeling tells me it's because of my poor memory and string management. Could someone kindly review the code and let me know if there's any obvious error? I would seriously very appreciate it.

Method 1
============

char* _LoggedInUserID;
char* _LoggedInUserName;
DWORD CALLBACK streamDisclaimer(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
	DWORD read;
	CRtfStream *st = reinterpret_cast<CRtfStream *>(dwCookie);
	const int MAX_USER_TAG = 10;

	char* userTag = (char*) malloc(strlen("<user>")+1);
	strncpy(userTag, "<user>", strlen("<user>"));
	userTag[strlen("<user>")] = '\0';

	size_t displayLen = strlen(_DlgLoggedInUserID);
	if(_DlgLoggedInUserName != NULL) displayLen += strlen(_DlgLoggedInUserName) + 3;  	char* displayStr = (char*) malloc(displayLen+1);

	if(_DlgLoggedInUserName != NULL){
		sprintf(displayStr, "%s (%s)", _DlgLoggedInUserName, _DlgLoggedInUserID);
	}else{
		sprintf(displayStr, "%s", _DlgLoggedInUserID);
	}
	displayStr[displayLen] = '\0';

	LPBYTE tmpBuff = new BYTE[cb + (displayLen*MAX_USER_TAG)];
	
	ReadFile(st->fileDisclaimer.get(), tmpBuff, cb, &read, NULL);
	string tmBuffStr = (char*) tmpBuff;
	size_t iPos      = tmBuffStr.find(string(userTag), 0);

	if(iPos != tmBuffStr.npos){
		int cnt = 0;
		while(iPos != tmBuffStr.npos && cnt < MAX_USER_TAG){
			tmBuffStr = tmBuffStr.erase(iPos, strlen(userTag));
			tmBuffStr.insert(iPos, displayStr);
			iPos = tmBuffStr.find(string(userTag), 0);
			cnt++;
		}
		int newLength = read - (strlen(userTag)*cnt) + (displayLen*cnt);
		strncpy((char*)pbBuff, tmBuffStr.c_str(), newLength);
		pbBuff[newLength] = '\0';
		*pcb = newLength;
	}else{
		strncpy((char*)pbBuff, (char*)tmpBuff, read);
		*pcb = read;
	}

	// clean up
	delete [] userTag;
	delete [] displayStr;

	userTag    = NULL;
	displayStr = NULL;
	tmpBuff    = NULL;

	return 0;
}

Method 2
=============

char* _DlgLoggedInUserID;
char* _DlgLoggedInUserName;

void CGina::setLoggedInUserID(PWSTR pszUserName){

	// free memory dynamically allocated
	delete [] _LoggedInUserID;  
	delete [] _LoggedInUserName;

	// deallocate memory that's not used
	_LoggedInUserID   = NULL;
	_LoggedInUserName = NULL;

	if(pszUserName != NULL)
	{
		int nstrlen = WideCharToMultiByte(CP_ACP, 0, pszUserName, -1, NULL, 0, NULL, NULL);
		PSTR UserNameStr = (PSTR)HeapAlloc(GetProcessHeap(),0,nstrlen);

		if(UserNameStr!=NULL){
			WideCharToMultiByte(CP_ACP, 0, pszUserName, -1, UserNameStr, nstrlen, NULL, NULL);
			_LoggedInUserID = (char*)malloc(nstrlen);
			strncpy(_LoggedInUserID, (char*)UserNameStr, nstrlen);
			for(int i = 0; i < nstrlen; i++){ 
				_LoggedInUserID[i] = toupper(_LoggedInUserID[i]);
			}
			HeapFree(GetProcessHeap(), 0, UserNameStr);
		}
	}
    try {
		DirectoryEntry* directoryEntryUser = new DirectoryEntry(S"LDAP://xxx.com.au/dc=xxx,dc=com,dc=au");
		DirectorySearcher* directorySearcherUser = new DirectorySearcher();
		directorySearcherUser->Filter = S"(objectClass=user)";
		directorySearcherUser->SearchRoot = directoryEntryUser;
		//directorySearcherUser->Filter = String::Concat(S"samaccountname=", _LoggedInUserID);

		char* filterStr = (char*) malloc(strlen("samaccountname=")+strlen(_LoggedInUserID)+1);
		sprintf(filterStr, "samaccountname=%s", _LoggedInUserID);
		filterStr[strlen("samaccountname=")+strlen(_LoggedInUserID)+1] = '\0';
		directorySearcherUser->Filter = filterStr;

		SearchResult* searchResultUser = directorySearcherUser->FindOne();

		if(searchResultUser->Properties->Contains(S"cn")){
			IEnumerator* myEnum = searchResultUser->Properties->Item[S"cn"]->GetEnumerator();
			while (myEnum->MoveNext()) {
				String* fullNameStrPtr = (String*)myEnum->Current;
				char* fullNameChar = (char*)(void*)Marshal::StringToCoTaskMemAnsi(fullNameStrPtr);

				_LoggedInUserName = (char*)malloc(strlen(fullNameChar)+1);
				strncpy(_LoggedInUserName, (char*)fullNameChar, strlen(fullNameChar));
				_LoggedInUserName[strlen(fullNameChar)] = '\0';

				delete [] fullNameChar;
				fullNameChar   = NULL;
				fullNameStrPtr = NULL;
				break;
			}
			myEnum = NULL;
		}
		delete [] filterStr;
		filterStr             = NULL;
		directoryEntryUser	  = NULL;
		directorySearcherUser = NULL;
		searchResultUser	  = NULL;

    } catch (Exception* e) {
		LDB1(L"<--setLoggedInUserID (%s)", e->Message);
	} 
}



Thanks guys


-- modified at 0:56 Wednesday 23rd November, 2005
Questionusing MSChart in VC++ and its methods Pin
nams_pr22-Nov-05 18:36
nams_pr22-Nov-05 18:36 
QuestionHow to create an HTML control and use it in a MDI Application(MFC) Pin
souvik04in22-Nov-05 18:19
souvik04in22-Nov-05 18:19 
AnswerRe: How to create an HTML control and use it in a MDI Application(MFC) Pin
includeh1022-Nov-05 18:53
includeh1022-Nov-05 18:53 
QuestionMultiple language support Pin
Harshabhi22-Nov-05 18:01
Harshabhi22-Nov-05 18:01 
QuestionHow to specify a DataGrid object (Windows.Forms)? Pin
FlyingZ22-Nov-05 17:39
FlyingZ22-Nov-05 17:39 
Questionyet another inpout32 parallel port question... Pin
indychris200622-Nov-05 17:28
indychris200622-Nov-05 17:28 
AnswerRe: yet another inpout32 parallel port question... Pin
kakan22-Nov-05 19:46
professionalkakan22-Nov-05 19:46 
GeneralRe: yet another inpout32 parallel port question... Pin
indychris200624-Nov-05 5:39
indychris200624-Nov-05 5:39 
GeneralRe: yet another inpout32 parallel port question... Pin
kakan24-Nov-05 20:26
professionalkakan24-Nov-05 20:26 
GeneralRe: yet another inpout32 parallel port question... Pin
indychris20065-Dec-05 15:00
indychris20065-Dec-05 15:00 
QuestionHelp with memcpy ? Pin
Cindy197822-Nov-05 15:30
Cindy197822-Nov-05 15:30 
AnswerRe: Help with memcpy ? Pin
Chris Losinger22-Nov-05 15:45
professionalChris Losinger22-Nov-05 15:45 
QuestionRe: Help with memcpy ? Pin
Cindy197822-Nov-05 16:01
Cindy197822-Nov-05 16:01 
AnswerRe: Help with memcpy ? Pin
Christian Graus22-Nov-05 16:26
protectorChristian Graus22-Nov-05 16:26 
AnswerRe: Help with memcpy ? Pin
Christian Graus22-Nov-05 16:31
protectorChristian Graus22-Nov-05 16:31 
Questioncreate ordinary window using menu Pin
stick_thai22-Nov-05 14:20
stick_thai22-Nov-05 14:20 
QuestionGDI+ PNG Transparency Pin
ClickHeRe22-Nov-05 13:50
ClickHeRe22-Nov-05 13:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.