Click here to Skip to main content
15,897,273 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: c++ win32, Trouble getting CreateThread and CreateProcess running together Pin
Richard Andrew x6425-Jan-12 9:34
professionalRichard Andrew x6425-Jan-12 9:34 
GeneralRe: c++ win32, Trouble getting CreateThread and CreateProcess running together Pin
jkirkerx25-Jan-12 9:56
professionaljkirkerx25-Jan-12 9:56 
GeneralRe: c++ win32, Trouble getting CreateThread and CreateProcess running together Pin
Richard Andrew x6425-Jan-12 9:59
professionalRichard Andrew x6425-Jan-12 9:59 
GeneralRe: c++ win32, Trouble getting CreateThread and CreateProcess running together Pin
jkirkerx25-Jan-12 10:04
professionaljkirkerx25-Jan-12 10:04 
QuestionRe: c++ win32, Trouble getting CreateThread and CreateProcess running together Pin
CPallini25-Jan-12 10:12
mveCPallini25-Jan-12 10:12 
AnswerRe: c++ win32, Trouble getting CreateThread and CreateProcess running together Pin
jkirkerx25-Jan-12 10:47
professionaljkirkerx25-Jan-12 10:47 
GeneralDone Pin
jkirkerx25-Jan-12 11:28
professionaljkirkerx25-Jan-12 11:28 
QuestionSHFileOperation with SHFILEOPSTRUCT fails when used with vars but works with string literals Pin
bcb_guy25-Jan-12 1:06
bcb_guy25-Jan-12 1:06 
Ok guys, I am totally puzzled with this one. I have been reading and reading and experimenting and I finally had to post.

Basically, whenever I call the FileCopy function below and I supply cSource with "C:\\Users\\UserTest\\Desktop\\File1.txt" and cDest with "C:\\Users\\UserTest\\Desktop\\File2.txt", instead of creating File2.txt ASCII file, the function generates an empty folder instead and names it File2.txt and the return value is 2. However, if I hard code the shFileOps.pFrom and shFileOps.pTo with the above paths the function works fine and the return value is 0! It even works when I have no '\0' chars with that one. What is wrong with my code???


I have the following code:

C++
int FileCopy(LPSTR cSource, LPSTR cDest)
{
	// Note - This string must be double-null terminated.
	cSource = Tools::NullifyString(cSource, true);
	cDest = Tools::NullifyString(cDest, true);

	LPCWSTR lpwstrSource = Convert::CharArrayToWideCharArray(cSource, false);
	LPCWSTR lpwstrDest = Convert::CharArrayToWideCharArray(cDest, false);

	SHFILEOPSTRUCT shFileOps;
	ZeroMemory(&shFileOps, sizeof(shFileOps));

	shFileOps.wFunc = FO_COPY;
	
	shFileOps.pFrom = lpwstrSource;
	shFileOps.pTo = lpwstrDest;

//	shFileOps.pFrom = L"C:\\Users\\UserTest\\Desktop\\File1.txt";
//	shFileOps.pTo = L"C:\\Users\\UserTest\\Desktop\\File2.txt";

	shFileOps.fFlags = FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_SILENT;
	shFileOps.fFlags |= FOF_ALLOWUNDO;


	return SHFileOperation(&shFileOps);

}

	// Inserts a NULL at the end of the string
	char *NullifyString(const char *cSource, bool DoubleNull = false)
	{
		char *cNew = NULL;

		// Get the length of the original buffer
		int SourceLength = strlen(cSource);

		// allocate enough space for all the chars + one more for the NULL char
		if (DoubleNull)
			cNew = new char[SourceLength + 2];
		else
			cNew = new char[SourceLength + 1];

		for (int i = 0; i < SourceLength; i++)
			cNew[i] = cSource[i];

		cNew[strlen(cSource)] = '\0';

		if (DoubleNull)
			cNew[strlen(cSource) + 1] = '\0';

		return cNew;
	}

	wchar_t *CharArrayToWideCharArray(const char *cSource, bool Nullify = true)
	{
		wchar_t *wcDest = NULL;

		if (Nullify) wcDest = new wchar_t[strlen(cSource) + 1];
		else wcDest = new wchar_t[strlen(cSource)];

		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cSource, strlen(cSource), wcDest, wcslen(wcDest));

		if (Nullify) wcDest[strlen(cSource)] = NULL;

		return wcDest;
	}


I checked the values in the converted vars returned by NullifyString and CharArrayToWideCharArray and I get everything as expected. I am using Visual C++ 2010 Express (and for some reason it forces me nearly everything to convert into wchar_t * for some reason when it is supposed to be a normal LPSTR according to MSDN docs)

modified 25-Jan-12 7:13am.

AnswerRe: SHFileOperation with SHFILEOPSTRUCT fails when used with vars but works with string literals Pin
Jochen Arndt25-Jan-12 1:55
professionalJochen Arndt25-Jan-12 1:55 
AnswerRe: SHFileOperation with SHFILEOPSTRUCT fails when used with vars but works with string literals Pin
Richard Andrew x6425-Jan-12 2:06
professionalRichard Andrew x6425-Jan-12 2:06 
AnswerIssue Resolved! (Of Course User Error) Pin
bcb_guy25-Jan-12 17:41
bcb_guy25-Jan-12 17:41 
GeneralRe: Issue Resolved! (Of Course User Error) Pin
Jochen Arndt25-Jan-12 21:20
professionalJochen Arndt25-Jan-12 21:20 
GeneralRe: Issue Resolved! (Of Course User Error) Pin
bcb_guy25-Jan-12 23:29
bcb_guy25-Jan-12 23:29 
GeneralRe: Issue Resolved! (Of Course User Error) Pin
David Crow26-Jan-12 3:06
David Crow26-Jan-12 3:06 
QuestionGet the local drives, scan and retrieve files using visual c++ Pin
amabi25-Jan-12 0:43
amabi25-Jan-12 0:43 
AnswerRe: Get the local drives, scan and retrieve files using visual c++ Pin
David Crow25-Jan-12 3:01
David Crow25-Jan-12 3:01 
QuestionXML Data To C++ Objects Pin
002comp24-Jan-12 22:24
002comp24-Jan-12 22:24 
AnswerRe: XML Data To C++ Objects Pin
Rolf Kristensen26-Jan-12 20:20
Rolf Kristensen26-Jan-12 20:20 
QuestionHow to check if an internet connection exist ? Pin
_Flaviu24-Jan-12 1:13
_Flaviu24-Jan-12 1:13 
AnswerRe: How to check if an internet connection exist ? Pin
Rajesh R Subramanian24-Jan-12 2:07
professionalRajesh R Subramanian24-Jan-12 2:07 
Questionusing extern in C Pin
Sakhalean23-Jan-12 22:53
Sakhalean23-Jan-12 22:53 
AnswerRe: using extern in C Pin
Jochen Arndt23-Jan-12 23:09
professionalJochen Arndt23-Jan-12 23:09 
GeneralRe: using extern in C Pin
Sakhalean23-Jan-12 23:32
Sakhalean23-Jan-12 23:32 
GeneralRe: using extern in C Pin
Jochen Arndt23-Jan-12 23:41
professionalJochen Arndt23-Jan-12 23:41 
AnswerRe: using extern in C Pin
Erudite_Eric24-Jan-12 5:33
Erudite_Eric24-Jan-12 5:33 

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.