5 Replies - 4208 Views - Last Post: 18 July 2012 - 10:11 AM Rate Topic: -----

#1 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Bad Type Conversion Somewhere

Posted 12 June 2012 - 02:52 AM

Hi, this is my first real time attempting C++, and I must say that this isn't my language of choice. The reason its in C++ is that I need it to work in a WinPE environment.

The code is supposed to loop through every letter of alphebet, and check if that path exists, then rename the original to original.old, and then replace with a local copy of the file. The way its meant to be used is to check every connected drive then update a file with a new one.

This works when I do lots of "If"s on whether the drive file exists, but I haven't yet got it to work as a loop. I think its something to do with my conversions from Char Charw and CStringW + CStringA. I was under the impression that it had to be a wide character for windows to support it as a file name / path. Anyway, the current code only returns a number in the command line. Any help would be much appreciated.

Imports:
#include <IO.h>
#include <Windows.h>
#include <string>
#include <iostream>



Code:
cout << "F--1";
			
			CString _Path=_T("bring.txt");
			CString _OldTag = _T(".old");
			CString _InPath=_T("Xcor.txt");
			LPCTSTR InPath = _InPath;
			LPCTSTR Path = _Path;
			LPCTSTR OldTag = _OldTag;

			char Letters[25] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','y','z'};
			int i;
			
	
			for(i= 0; i < 25; i++){
				CString FullPath = (Letters[i] + Path);
				LPCTSTR PathI = FullPath;
				
				
			const size_t newsizew = (FullPath.GetLength() + 1)*2;
			char *FPath = new char[newsizew];
			size_t convertedCharsw = 0;
			wcstombs_s(&convertedCharsw, FPath, newsizew, FullPath, _TRUNCATE );





				cout << "\n Attempting Path: ";
				cout << FullPath;
					CString _Path=_T("f");
					 LPCTSTR Path = _Path;
					
					 if (_access (FPath, 0) != 0) {
						 cout << "F1";
						 break;
			}
					 cout << "F2";
					 // File Exists and we are able to modify.
					 (CopyFile(FullPath,FullPath + OldTag,false)); // Rename from file.xxx to file.xxx.old
					 CFile::Remove(FullPath); // Delete old path.
					 (CopyFile(InPath,FullPath,false)); // Copy from local to remote drive.
					 //done
			}



The file names in there were purely for debugging purposes, so I could run it all in one folder rather than eight different drives.

Is This A Good Question/Topic? 0
  • +

Replies To: Bad Type Conversion Somewhere

#2 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Bad Type Conversion Somewhere

Posted 16 July 2012 - 03:46 PM

I am getting closer to understanding. For all those in the same situation, the issue relies on my understanding of LPCTSTR, and what it is. There is a useful article here that could help you understand. Will post a solution when I can.
Was This Post Helpful? 0
  • +
  • -

#3 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Bad Type Conversion Somewhere

Posted 16 July 2012 - 04:13 PM

I'm really confused now, what is with this unicode stuff.

Okay, from what I understand, the problem with the first code is that I was actually passing pointer locations so was getting numbers as the result, which obviously weren't valid file names.

The problem I am now getting is how to correctly get from
char
or
std::string
to
TCHAR
or
LPCTSTR


Since technically LPCTSTR is just a TCHAR pointer, I think, I just don't understand how it works. I tried converting char[] to TCHAR[] with the following code:

	TCHAR* ConvertTCHAR(char Char[])
		{
			USES_CONVERSION;
			TCHAR* b = A2T(Char);
			return b;

		}



But I don't think that this is that clean.

Anyway, according to the website that I mentioned in my previous post:

#define  LPCTSTR        const TCHAR* 



Since that is what I want, shouldn't this function be returning, essentially, what I want?

If so where is my confusion with pointers, because somewhere else said that std::string was a char[]*

ahhhh... so confused.

Anyway be, below is the code I have tried and failed to use.

	char* CombineChar(char* Char1, char* Char2)
	{
		char* mHeader = Char1;
		// retrieve incoming string and assign to variable
		char* msgHeader = new char[strlen(Char2)+1];
		memcpy(msgHeader,Char2,strlen(Char2)+1);
		msgHeader[strlen( Char2 )] = '\0';

		char* retVal = new char[strlen(mHeader)+strlen(msgHeader)+1];
		*retVal = '\0';
		
		// Assemble the string
		strcat(retVal,mHeader);
		strcat(retVal,msgHeader);

		return retVal;
	}

		TCHAR* ConvertTCHAR(char Char[])
		{
			USES_CONVERSION;
			TCHAR* b = A2T(Char);
			return b;

		}

	void AllDrives_Test(char SPath[])
	{
					char Letters[25] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','y','z'};
					int i;
			
	
			for(i= 0; i < 25; i++){
				
				string* PP = Letters[i] + SPath;
				ConvertTCHAR(PP);
				LPCTSTR Iglo = (TCHAR*)PP.c_str();
				char Ni[] = Letters[i];
				char Path = CombineChar(Ni ,SPath);
				LPCTSTR PathI = (PP);
				cout << PathI;


			/*


				cout << "\n Attempting Path: ";
				cout << FullPath;
					CString _Path=_T("f");
					 LPCTSTR Path = _Path;
					
					 if (_access (FPath, 0) != 0) {
						 cout << "F1";
						 break;
			}
					 cout << "F2";
					 // File Exists and we are able to modify.
					 (CopyFile(FullPath,FullPath + OldTag,false)); // Rename from file.xxx to file.xxx.old
					 CFile::Remove(FullPath); // Delete old path.
					 (CopyFile(InPath,FullPath,false)); // Copy from local to remote drive.
					 //done
					 */
				break;
			}




(Note: I switched to a multibyte build to see if it would make a difference. It didn't.)

Apon reflection this is in the wrong place. It should be in the C/C++ forum, its not .net. Whoops. Sorry. Could this be moved?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Bad Type Conversion Somewhere

Posted 18 July 2012 - 09:22 AM

Part of your problem seems to be not understanding the various Microsoft typedefs. I suggest you start at this link: MSDN Common Data Types and see if any of this information helps.

A TCHAR.

A TCHAR can be either a wide or normal character depending on if Unicode is involved.

Quote

If so where is my confusion with pointers, because somewhere else said that std::string was a char[]*


A std::string is not the same as a C-string (A zero terminated character array). A std::string can be
converted into a const C-string with the std::string.c_string() member function.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Bad Type Conversion Somewhere

Posted 18 July 2012 - 10:03 AM

This will get you into serious trouble:
	TCHAR* ConvertTCHAR(char Char[])
	{
		USES_CONVERSION;
		TCHAR* b = A2T(Char);
		return b;

	}



USES_CONVERSION is a macro that declares a couple of local variables one of which is a local buffer. The A2T() macro may use that local buffer and return a pointer to it. You are just turning around and returning a pointer to that buffer that exists on the stack.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Bad Type Conversion Somewhere

Posted 18 July 2012 - 10:11 AM

What are you trying to do here?
            LPCTSTR Iglo = (TCHAR*)PP.c_str();



PP is a standard C++ std::string. It is not the MFC CString class that will do conversions to UNICODE or ANSI as needed when you do a typecast.

With the code above you are telling the compiler, "I know that PP.c_str() returns a pointer to an ANSI string. Trust me when I tell you that it is actually a pointer to a UNICODE string. I know what I'm doing."
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1