Solved

Copying data(transferring) from TCHAR sstring[MAX_PATH ] to a CString str?

Posted on 2009-04-14
12
479 Views
Last Modified: 2013-11-20
What is the exact process( show MFC code please) that I should use to transfer(copy) the data from TCHAR sstring[MAX_PATH]  to CString str?

I use CStdioFile nerely exclusively and I need my data to be contained in a CString; but RegOpenKeyEx and RegSetValue require TCHAR (I thought CString was already a TCHAR equalevant ).
   
0
Comment
Question by:LarryMillsSr
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 300 total points
Comment Utility
Hi LarryMillsSr,

in general you're right, CString is implemented based on TCHAR. If the functions require a TCHAR string it might be needed that you explicitley cast the CString to be a LPCTSTR. You can do this by just using a simple cast since CString has an implemented LPCTSTR-operator, so i.e. this code should IMO work:

CString str = ...;
RegOpenKeyEx( ..., (LPCTSTR)str, ... );

Hope that helps,

ZOPPO
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
Comment Utility
I guess your problem isn't TCHAR but a mix of UNICODE and ASCII.
Have a look at mbtowcs (and associated functions to convert between the two).

ps.  psuedo code
TCHAR s[255] = _T("Hello");
CString s2(s);
s is now Hello
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
sorry s2 is now Hello
0
 

Author Comment

by:LarryMillsSr
Comment Utility
I tried the cast before I contacted Experts Exchange. It didn't work.  Here's a brief description of my problem:  The purchased installer sets four Register Keys as shown:
HKEY_LOCAL_MACHINE\Mills Software Solutions Inc\Notify
  Company   REG_SZ  MSS Inc
  Name         REG_SZ  John Doe
  Serial         REG_SZ  1111122222333334444455555
  RegCode   REG_SZ  AAAAABBBBBCCCCCDDDDD

What I need to do in my program is capture each of these and make a duplicate of them so I can use the data in my Updates( and other stuff).  I need that data, because some of that data was entered by the User and I have no other means of retrieving it.  How do I do this in MFC?  winbase.h and windows.h are included(in a of-handed way in afxwin.h(MFC) but MFC does not allow(or I can't get it to do so) using regular RegOpenKeyEx, RegQueryValueEx, etc. . Note: my program is ANSI not UNICODE.
0
 

Author Comment

by:LarryMillsSr
Comment Utility
Should've been:(typo error)

HKEY_LOCAL_MACHINE\Software\Mills Software Solutions Inc\Notify

Note: CWinApp only allows entries in HKEY_CURRENT_USER.

0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
It is really just as direct and easy as AndyAiinscow said.
I guess you are getting errors in your calls to RegOpenKeyEx, RegQueryValueEx, etc.   If so, then please provide the error number -- the value returned by the first API function that fails.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Expert Comment

by:LarryAMillsSr
Comment Utility
ERROR_INVALID_FUNCTION (2)
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
A result of 2 is  ERROR_FILE_NOT_FOUND
You have misspelled or otherwise incorrectly entered the name of the subkey.  The way to solve this is as follows:
Open RegEdit and find the thing you want to see.  I'm thinking it will be in:
HKEY_LOCAL_MACHINE
    SOFTWARE
        MSS Inc
           Notify
Below Notify will be the three items you want.  If you can't see them, then you are looking in the wrong place, or they have not been set.  Once you find the right place, right click Notify and choose "Copy Key Name"
Go to your program and paste it on a blank line:
    HKEY_LOCAL_MACHINE\SOFTWARE\MSS Inc\Notify
Modify it by removing the root key and doubling all backslashes:
     SOFTWARE\\MSS Inc\\Notify
Now you can use it in RegOpenKeyEx and it will succeed.   See the Attached example, using a key from a common utility program.
-- Dan

void CD55Dlg::OnButton1() 

{

	HKEY hKey;

	LONG nResult=RegOpenKeyEx(HKEY_LOCAL_MACHINE, 

		"SOFTWARE\\Adobe\\Adobe Acrobat\\7.0\\Installer",

		0, KEY_QUERY_VALUE, &hKey);
 

	unsigned char buffer[_MAX_PATH];

	unsigned long datatype;

	unsigned long bufferlength = sizeof(buffer);

 

	nResult= RegQueryValueEx( hKey, "Path", NULL, &datatype, buffer, &bufferlength );
 

	MessageBox( (char*)buffer, "The value of 'Path' is..." );

}

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
A guess - you have
"xx\yy\zz" instead of "xx\\yy\\zz" - note the double \ character
0
 

Author Comment

by:LarryMillsSr
Comment Utility
Question:
After you have the "root Path Key" how exactly do you obtain the four varables data? and at what point do you close the key you've opened == ie, after each valve's data is received or after all value's(4) data is received?
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
open key
read value
read value
....
close key

else you attempt to read from a closed key - ERROR
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
>>how exactly do you obtain the four varables data?
Pleasae look at the example that I provided.  The value ends up in the variablle named buffer
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now