|
I want to convert a string to LPWSTR to be used in RegOpenKeyEx.
i had been able to do it to the string directly i.e. L"xxx"
but unable to do it using string variable.
how can i do that please help.
|
|
|
|
|
|
If performance is not important, I think the easier alternative way is using of _bstr_t type. You can assign and you can get strings of both types:
_bstr_t s;
s = "abc";
s = L"abc";
char * a = s;
wchar_t * w = s;
So just try ::RegOpenKeyEx(..., _bstr_t(my_non_unicode_string), ...) .
Note that you can explicitly stipulate the version of RegOpenKeyEx function when your strings do not correspond to the project configuration settings. Just use RegOpenKeyExA or RegOpenKeyExW according to your needs.
|
|
|
|
|
So just try ::RegOpenKeyEx(..., _bstr_t(my_non_unicode_string), ...).
this does not work.. gives error at _bstr_t
please help
|
|
|
|
|
The _bstr_t type is defined in comutil.h, so you have to add:
#include <comutil.h>
If this is not enough and you get linker errors, probably you will have to add comsuppw.lib library to project settings (Linker --> Input --> Additional Dependencies).
|
|
|
|
|
i tried that and now it is giving me error: unable to convert from standard string to _bstr_t.
LONG retcode = RegOpenKeyEx(HKEY_CLASSES_ROOT,_bstr_t(ext.toStdString()), 0, KEY_ENUMERATE_SUB_KEYS, ®key);
tell me wat should I do. I am working on QT development env.(based on C++)
|
|
|
|
|
I suppose ext.toStdString() returns a std::string . In this case, you should try ext.toStdString().c_str() . Otherwise investigate which kind of value returns toStdString() .
|
|
|
|
|
|
Hi,
I am having a problem with the collection class CStringList
In the part below, coded in vc++6, when the program is executen and StrList.AddTail(pStr[i]) is reached, the error 'this program has performed an illigal operation..." comes.
Although while compiling, no error is shown.
Please can you tell me what is going wrong?
<br />
void Cop1::OnAdd() <br />
{<br />
int iCount = 0;<br />
int i;<br />
POSITION p;<br />
<br />
iCount = m_payeeList.GetSelCount();<br />
<br />
if (iCount <= 0) return ;<br />
<br />
int* pItems = new int [iCount];<br />
m_payeeList.GetSelItems(iCount, pItems);<br />
<br />
CStringList StrList;<br />
<br />
char *pStr[10];<br />
<br />
for (i=0;i<iCount; i++)<br />
{<br />
<br />
m_payeeList.GetText(pItems[i], pStr[i]);<br />
StrList.AddTail(pStr[i]);<br />
<br />
}<br />
}<br />
Thanks.
Tara
Fortitudine Vinsinues!
-- modified at 10:20 Tuesday 13th June, 2006
|
|
|
|
|
I think instead of
m_payeeList.GetText(pItems[i], pStr[i]);
StrList.AddTail(pStr[i]);
you should try
CString text;
m_payeeList.GetText(pItems[i], text);
StrList.AddTail(text);
Now you do not need pStr , which actually was not initialized.
Hope it helps.
|
|
|
|
|
Ah! That works. But I did initialised pStr:
char *pStr[10];
How do I get the string back form the StringList?
The code below is giving error. Is it correct?
POSITION p;<br />
CString s;<br />
s.Format("%s",StrList.GetNext(p)); <br />
AfxMessageBox(s,MB_OK);
Tara
Fortitudine Vinsinues!
|
|
|
|
|
I think you missed to initialize p . The following should work:
POSITION p = StrList.GetHeadPosition();
CString s = StrList.GetNext(p);
AfxMessageBox(s,MB_OK);
|
|
|
|
|
Yes! It works! Thanks a billion!
Tara
Fortitudine Vinsinues!
|
|
|
|
|
Can I not pass the String list, StrList to a function?
It is not working when I am passing it like usual function.
I get the error 'FillReportList' : cannot convert parameter 1 from 'class CStringList' to 'class CStringList'
FillReportList(StrList);<br />
<br />
FillReportList(CStringList list)<br />
{<br />
}<br />
Fortitudine Vinsinues!
|
|
|
|
|
If you need to read some values from the list, try this:
FillReportList(const CStringList <big>&</big> list)
{
} If you need to modify the list, try this:
FillReportList(CStringList <big>&</big> list)
{
}
|
|
|
|
|
Thanks. Please can you explain the consept to me? Why is it that when we use a CString object we don't put & while for CStringList we do? Is it because it is a list?
Fortitudine Vinsinues!
|
|
|
|
|
When you make a function like FillReportList(CStringList list) or FillReportList(int x) , the system actually tries to make a copy of passed values (on the "stack"). Within the function, you work with the copy.
Copying is possible for int and other types, but is not possible for CStringList . This is because CStringList is a class which does not have a "copy constructor".
Disabling of copy operation for CStringList was decided by the authors, since frequently CStringList can have big size, and therefore multiple copying can be problematic in your application from the memory usage viewpoint.
Therefore we cannot pass a CStringList object by making a copy. In this case, we should pass a reference to the object, using "& " in argument declaration. Now the system does not make a copy of your list, but simply passes a small reference (an address, a pointer).
|
|
|
|
|
Thanks a lot for explaining the concept so nicely. Now I understand why we pass some object this way. Earlier, I used to do tria-and-error to see what works!!
Tara
Fortitudine Vinsinues!
|
|
|
|
|
TheinstruTara wrote: But I did initialised pStr:
char *pStr[10];
Only if you had done something like:
for (i = 0; i < iCount; i++)
{
pStr[i] = new char[100];
m_payeeList.GetText(pItems[i], pStr[i]);
}
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
int iCount = 0;
int i;
iCount = m_payeelist.GetSelCount();
if (iCount <= 0) return ;
int* pItems = new int [iCount];
m_payeelist.GetSelItems(iCount, pItems);
CStringList StrList;
for (i=0;i<m_payeelist.GetSelCount();i++)
{
char *pStr=new char[10];
m_payeelist.GetText (pItems[i],pStr);
StrList.AddTail(pStr);
delete pStr;
}
See if this helps
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
-- modified at 10:38 Tuesday 13th June, 2006
|
|
|
|
|
It works this way too. I never seem to understand how to work with char correctly.
Thanks.
Fortitudine Vinsinues!
|
|
|
|
|
Hello everyone!
This is what I want:
fileName : is730enu.exe.mtx [Correct]
unexName : is730enu.exe [Adds 4 more chars at end]
partName : is730enu.exe.000 [Same as above, but with .000 ext]
How could I do that? Like, I want to remove the last 4 chars from fileName and add that to unexName, but then add the .000 extension on the 4 chars left, but it just adds it after... I don't get it, how can it add it if I have the string on a fixed size? Thanks!
<br />
char *partName, *unexName;<br />
<br />
<br />
unexName = malloc(strlen(fileName) - 4);<br />
strncpy(unexName, fileName, strlen(fileName) - 4);<br />
<br />
<br />
partName = malloc(strlen(fileName));<br />
partName = mtxSplitAddExt(unexName, part);<br />
<br />
return partName;
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
|
|
|
|
|
Any reason why you can't use std::string?
You have a memory leak for unexName and you have delegated responsiblity to the caller to free partName.
|
|
|
|
|
hfry wrote: Any reason why you can't use std::string?
It's regular C.
hfry wrote: You have a memory leak for unexName and you have delegated responsiblity to the caller to free partName.
So... What can I do about it? Thanks! (Sorry if I sounded n00bish, but I am after all... )
Lord Kixdemp
www.SulfurMidis.com
www.SulfurSoft.tk
[ftp://][http://][hotline://]tsfc.ath.cx
-- modified at 10:26 Tuesday 13th June, 2006
|
|
|
|
|
Your problem:
a) strlen returns the length of the string WITHOUT null-termination.
leads to
b) you are allocating only enough memory for the "text", not for the terminator.
and
c) strncpy realizes that there is not enough room for the null-terminator (remember, strncopy copies AT MOST n characters - including the null terminator. If you had used strcpy, you would instead have written outside your allocated memory.
Solution: use (strlen(input)+1) for mallocing and strncpy.
Cheers,
Sebastian
Note: Before someone shouts "but the mtx extension is only 3 characters", let me add: there is a dot in there as well.
--
Contra vim mortem non est medicamen in hortem.
-- modified at 10:45 Tuesday 13th June, 2006
|
|
|
|