|
Thanks for your concern.
Yes i use MFC.
By your question, I got an idea to convert it into a CString and apply delete and then save it back - something like this;
<br />
unsigned char *String;<br />
<br />
CString cString,result;<br />
cString="{abcdef}";<br />
int n=cString.GetLength();<br />
result=cString.Mid(1,n-2);<br />
<br />
String = (BSTR)rest; <br />
However, here i am not able to store it back to 'String'. i am getting various compiler errors like
" error C2440: 'type cast' : cannot convert from 'class CString' to 'unsigned short *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called "
Need help in typecasting in the last line.
Priya Sundar
|
|
|
|
|
What is rest , and why are you trying to cast it into a BSTR , when what you wand is a unsigned char* ?
And why unsigned char ? Thats uncommon.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal. George Orwell, "Keep the Aspidistra Flying", Opening words
|
|
|
|
|
|
You want to convert a GUID to a String?
Try somthing like this:
void GetFormattedGuid( const GUID& guid, CString& rString)
{
const CString strFormat(_T("%08lX-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X"));
rString.Format(strFormat,
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5], guid.Data4[6], m_guid.Data4[7],
);
} When you are using MFC anyway, there is no point in not using it to help you.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal. George Orwell, "Keep the Aspidistra Flying", Opening words
|
|
|
|
|
Sorry, i want to convert a character array to GUID
<br />
bool CTcpIpPropertiesChangerApp::UUidConversion()<br />
{<br />
char szValue[MAX_GUID_STRING_LEN];<br />
<br />
szValue={A89377EA-D4ED-4A03-A37F-4A9DE785E1A9};
<br />
unsigned char *GuidString=new unsigned char[MAX_GUID_STRING_LEN];<br />
GuidString[MAX_GUID_STRING_LEN - 2] = _T('\0');<br />
<br />
for(int l=0;l<MAX_GUID_STRING_LEN;l++)<br />
GuidString[l] = szValue[l];<br />
<br />
GUID ret = { 0 };
if(UuidFromString(GuidString,&ret)==RPC_S_OK)<br />
{<br />
delete GuidString;<br />
return true;<br />
}<br />
else<br />
return false;<br />
}<br />
Now this function is always returning false as UuidFromString is always returning RPC_S_INVALID_STRING_UUID telling that the string UUID/GUID 'GuidString' is invalid.
Priya Sundar
|
|
|
|
|
Priya_Sundar wrote: Now this function is always returning false as UuidFromString is always returning RPC_S_INVALID_STRING_UUID telling that the string UUID/GUID 'GuidString' is invalid.
Wow.
This is the first time you give a proper error report.
You never mentioned this function before.
OK. For me
#include "stdafx.h"
#include <atlstr.h>
#include <iostream>
#include <RPC.h>
#pragma comment( lib, "Rpcrt4")
int _tmain(int argc, _TCHAR* argv[])
{
const char szValue[] = "{A89377EA-D4ED-4A03-A37F-4A9DE785E1A9}";
CString szGuidString( szValue+1, strlen(szValue)-2);
GUID ret = { 0 };
const RPC_STATUS status = UuidFromString( (unsigned char*)(LPCTSTR)szGuidString, &ret);
if( status == RPC_S_OK)
{
...
}
else
{
...
}
return system("pause");
} worked.
Note the double cast in the UuidFromString-call: It is used to get the raw const TCHAR* from the CString , and the other cast tells the compiler "Shut up and take this as if it were unsigned s!".
That is not Unicode-compatible. Under Unicode, the CString would hold wchar_t s, and by forcing the compiler to tread them as a string of unsigned char s, anything could happen.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal. George Orwell, "Keep the Aspidistra Flying", Opening words
|
|
|
|
|
sorry rest is 'result'
and i started trying with (unsigned char *) and ended up with the error
"error C2440: 'type cast' : cannot convert from 'class CString' to 'unsigned char *'"
so i started using various things like BSTR. And that is where i need help!
Priya Sundar
|
|
|
|
|
cast to (LPCTSTR) instead
|
|
|
|
|
The normal plain old C-String most function want is char* or const char* . That is a signed type.
Microsoft uses a specific macro named TCHAR , which boils down to char* in non-unicode and to (2 Byte-)wchar_t in Unicode builds. Very handy.
Likewise, TSTR denotes a null-terminated string consisting of TCHAR.
A pointer to a constant string consisting of TCHAR is called a LPCTSTR (long pointer to constant TSTR ).
The CString-class wraps the TCHAR s from you, and has a conversion operator to be used in place of a constant TSTR .
So, when you stop using unsigned char and start using TCHAR , you problems will go away automagically.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal. George Orwell, "Keep the Aspidistra Flying", Opening words
|
|
|
|
|
Priya_Sundar wrote: However, here i am not able to store it back to 'String'.
Why does that surprise you? No memory has been allocated for String . As it stands now, it points to wherever. try:
String = new unsigned char[result.GetLength() + 1];
strcpy(String, result);
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
sorry for the delayed post.
i was actually in a meeting.
to your problem,
Priya_Sundar wrote: However, here i am not able to store it back to 'String'. i am getting various compiler errors like
the quickest solution is ,
for(int i=0;i less than n;i++)
{
String[i]=cString.Getat(i);//or cString[i] must also should work.
}
gud luck.
-- modified at 10:08 Thursday 11th October, 2007
Suggestion to the members:
prefix your main thread subject with [SOLVED] if it is solved.
chandu.
|
|
|
|
|
i think the topic got far diverted here.
|
|
|
|
|
OK firstly we need to establish the meaning of 'delete' in this context. In C++ delete tends to means 'free the memory'. You certainly can't 'free the memory' currently being used by the '{' and the '}' so in C++ terms you can't 'delete' the first and last characters.
What you want is to end up with String pointing at "abcdef". You could do this with 2 lines
<br />
String++;<br />
String[6] = '\0';<br />
This will make the charatcer pointer point at the next character and then 'blank out' the end '}' (which is 6 characters on from the 'a' that String now points at) by replacing it with a NULL
There is one problem with this of course. You now have no way to free the string memory as the original value of String is lost. If your string is on the heap you will need to solve this. Enjoy.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
use a temporary buffer and use the CRT function strcpy() to copy what you want in the origin string
or if you use C++, you'd better look into the std::string STL class
|
|
|
|
|
i tried something like this
<br />
::string gidstring = String;<br />
gidstring = gidstring.substr(1, gidstring.length() - 2);<br />
But ended up with compiler error:
"error C2039: 'string' : is not a member of '`global namespace''"
inspite of including the <string.h> header file.
how to proceed??
Priya Sundar
|
|
|
|
|
why did you write ::string , thereby explicitly requesting string in the global namespace?
You probably wanted to write std::string .
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal. George Orwell, "Keep the Aspidistra Flying", Opening words
|
|
|
|
|
What about...
unsigned char *srcString = "{abcdef}";
unsigned char *dstString = "";
memcpy (srcString+1, dstString, (srcString.length () - 2));
If it works you can still free the memory and delete the pointer avoiding the problem that Matthew said.
Up to that point you can work with *dstString as it was your original one.
Hope it helps
P.S. Im not sure if it works, I just asked about buffers and how to get parts of the memory two/three days ago, and this is something that I get from the answers I received.
-- modified at 6:29 Thursday 11th October, 2007
I forgot...
with the srcString + X you specify the first character you want without changing the "value" of where the pointer points.
at dsstString you can make the same leaving some intial chars unmodified adding X to his base adress
the last is the number of chars of the first string you wanna copy in the second
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
I want to use this function to get close event from WORD(like DoModal() in Dialog). My code like this:
---------------------------------------------------------------------------------------------
COleDispatchDriver DocProperties(pDispatch, FALSE);
_variant_t Property((long)Word::wdPropertyPages);
_variant_t Result;
DocProperties.InvokeHelper(DISPID_....,
DISPATCH_...,
VT_BSTRT,
(void*)&Result,
(BYTE*)VTS_VARIANT,
&Property);
---------------------------------------------------------------------------------------------
Is it correct idea? And what I have to input for 3 first parameters? Thank you.
|
|
|
|
|
hai,
iam using a 64 bit integer in my application.
now my problem is if i want to print its hexa value into a string, like
str.Format("%x",value);
it is printing only lsb 32 bits.
how can i achieve this using a single type specifier like %x.
how ever,
i got it solved with the following technique.
UINT64 value=0xabcdefef;
CString str;
unsigned long value1,value2;
value1=value>>32;
value2=value;
str.Format("%x%x",value1,value2);
can any body please suggest me what is the type specifier for unsigned int 64.
thanks in advance.
-- modified at 4:59 Thursday 11th October, 2007
|
|
|
|
|
I am not sure about but... can it help casting the value to (_UINT64)? I mean
str.Format (_T("%x"), (UINT64) value);
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
|
thanks nibu,
what i was looking for was exctly that.
|
|
|
|
|
Hello friends,
I require to show the dc into the Picture Control as a bitmap.
For that I am using the code like below, but only shows blank bitmap in the Picture control.
HDC DeskDC = GetDC(NULL);
HDC hdcStatic = GetDC(hWndStatic);
HBITMAP hb = CreateCompatibleBitmap(hdcStatic,200,200);
SelectObject(hdcStatic,hb);
SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
SendMessage(hWndStatic, STM_SETIMAGE , IMAGE_BITMAP,(LPARAM)hb);
Please tell me if you know what is going wrong.
|
|
|
|
|
What are you trying to do???
You want the bitmap to be a copy of a a 200x200 rect of the screen?
That's fine.
You should select the hb bitmap back out of the memory DC before you
hand it off to the static (picture) control.
<font color="Red">HGDIOBJ hOldBM = </font>SelectObject(hdcStatic,hb);
SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
<font color="Red">SelectObject(hdcStatic, hOldBM);</font>
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thnk you vary much for you help. But still I am not able to perform the thing.
I am doing something like this as you have told me to do.
But getting NULL in hOLdBM. and So getting BLANK Image (black Image)
HDC DeskDC = GetDC(NULL);
HDC hdcStatic = GetDC(hWndStatic);
HBITMAP hb = CreateCompatibleBitmap(hdcStatic,200,200);
HGDIOBJ hOldBM = SelectObject(hdcStatic,hb);
SelectObject(hdcStatic, hOldBM);
SetWindowLong(hWndStatic,GWL_STYLE,(GetWindowLong(hWndStatic,GWL_STYLE) & ~SS_TYPEMASK) | SS_BITMAP) ;
BitBlt(hdcStatic,0,0,200,200,DeskDC,0,0,SRCCOPY);
SelectObject(hdcStatic, hOldBM);
SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hOldBM);
SendMessage(hWndStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hb);
-- modified at 2:00 Friday 12th October, 2007
|
|
|
|
|