16. About COM type conversion question??
I have defined two variables, one is void *piaRef=new unsigned char[1000]; the other is m_Temp=new CComVariant(); My question is how to get the value in piaRef
COPY into m_Temp.
Reply: nichang() (2001-11-21 15:34:04) Score 0 points
CComBSTR bsRef=piaRef;
m_Temp=()
Reply: VincentChin (Plassy God) (2001-11-21 17:04:24) Score 0 points
CComBSTR bsRef=piaRef;
//error C2440: 'initializing' : cannot convert from 'void *' to 'class ATL::CComBSTR'
m_Temp=();
//error C2440: '=' : cannot convert from 'unsigned short *' to 'class ATL::CComVariant *'
Reply: nichang() (2001-11-21 17:14:28) Score 0 points
Change void* to unsigned char*
Reply: VincentChin (Plassy God) (2001-11-21 17:22:22) Score 0 points
I use CComBSTR bsRef=(unsigned char*)piaRef, won't it work?
Reply: VincentChin (Plassy God) (2001-11-21 17:28:06) Score 0 points
An error will be reported:
error C2440: 'type cast' : cannot convert from 'unsigned char *' to 'class ATL::CComBSTR'
Reply: nichang() (2001-11-22 9:12:14) Score 0 points
m_Temp=::SysAllocString((OLECHAR *)piaRef)
Reply: VincentChin (Plassy God) (2001-11-22 10:43:07) Score 0 points
//error C2440: '=' : cannot convert from 'unsigned short *' to 'class ATL::CComVariant *'
Reply: VincentChin (Plassy God) (2001-11-22 11:22:35) Score 0 points
m_Temp=new CComVariant(::SysAllocString(OLECHAR *)piaRef)); No error occurred, but my m_Temp is a PROPERTY in the COM component, what I want to return is
unsigned char type (single byte), but after the above conversion, it is no longer a single byte! what to do?
Reply: jiangsheng (Jiang Sheng) (2001-11-22 11:36:58) Score 0 points
Change the type of this property to BSTR
Reply: GrayWhite (GrayWhite) (2001-11-22 12:01:09) Score 0 points
m_Temp = new CComVariant((char*) piaRef); VB is used as BSTR, who do you want to use it for? VC does not use VARIANT.
Reply: GrayWhite (GrayWhite) (2001-11-22 12:18:18) Score 19 points
Oh, I get it, you want each byte array:
SAFEARRAY *psa = SafeArrayCreateVector(VT_UI1, 0, 1000);
if (!psa)
_com_issue_error(ERROR_NOT_ENOUGH_MEMORY);
HRESULT hr
for (long i = 0; i < 2; i ++)
{
if (FAILED (hr = SafeArrayPutElement(psa, &i, piaRef + i)))
_com_issue_error(hr);
}
_variant_t va; // include <>
= VT_ARRAY | VT_UI1;
= psa;
m_Temp = new CComVariant(va);
Reply: VincentChin (Plassy God) (2001-11-22 14:21:08) Score 0 points
SafeArrayPutElement(psa, &i, piaRef + i)
//error C2036: 'void *' : unknown size
Reply: VincentChin (Plassy God) (2001-11-22 14:46:05) Score 0 points
To GrayWhite: Why do you need for(long i=0;i<2;i++)?
Reply: nichang() (2001-11-22 15:16:35) Score 0 points
How do you want to convert it? Do you copy the values in the array into CComVariant and save them as a string?
Reply: VincentChin (Plassy God) (2001-11-22 15:28:35) Score 0 points
I want to return the value in piaRef to other programs as is. What I'm doing is a COM component. Thank you everyone!
Reply: nichang() (2001-11-22 15:34:40) Score 10 points
unsigned char *s=new unsigned char[1000];
strcpy((char*)s,"1234");//You can set the value in s by yourself.
BSTR bstrS;
oleS=A2WBSTR((char*)s);//Convert char* to BSTR type
CComVariant comVT;
comVT=oleS;//Convert BSTR to CComVariant, one step here is OK, comVT=A2WBSTR((char*)s);
Reply: VincentChin (Plassy God) (2001-11-22 16:54:07) Score 0 points
Thank you!
But I have another question, which is how can I return if there is something like '/0' in s? char *If you encounter '/0', you will think it's over. The complete design is like this. I define a void * to obtain data from an external device. The data should be an unsigned char. I want to pass the returned data as a property for other applications to use (such as VB).
Reply: nichang() (2001-11-22 17:18:09) Score 0 points
It's OK to convert '/0' to other things like '/1'.
Reply: jiangsheng (Jiang Sheng) (2001-11-22 18:07:16) Score 0 points
Use string arrays
Reply: VincentChin (Plassy God) (2001-11-23 15:54:39) Score 0 points
Thank you for your reply! My problem has been solved! as follows:
SAFEARRAY *psa = SafeArrayCreateVector(VT_UI1, 0, 1000);
if (!psa)
return S_FALSE;
HRESULT hr;
for (long i = 0; i < 1000; i ++)
if (FAILED (hr = SafeArrayPutElement(psa, &i, ((unsigned char*)piaRefTemplate) + i)))
return S_FALSE;
VARIANT va;
= VT_ARRAY | VT_UI1;
= psa;
CComVariant *m_Temp = new CComVariant();
m_Temp->Copy(&va);
17. The difference between type conversion static_cast reinterprete_cast??
Differences between static_cast reinterprete_cast
Reply: tar(GPS) (2001-11-21 10:06:41) Score 0 points
static_cast checks the correlation of the conversion type's healthy
If there is no drawing, there will be a compilation error
Reinterprete_cast is just a hard turn
Reply: tigerwoods(tao) (2001-11-21 12:28:19) Score 0 points
Is it possible to understand this way: in multiple inheritance, static_cast can implement the movement of object pointers, thus pointing to the correct parent object part, while reinterprete_cast does not have an offset?
Reply: liu_feng_fly (I hate the drivers so much, hey, just to make a living) (2001-11-21 12:35:14) Score 0 points
Dynamic_cast can be used in multiple inheritance
18. So how do you get the string in CString? ?
Reply: nichang() (2001-11-5 17:06:00) Score 0 points
=(LPCTSTR)CString variable
Reply: snake1122 (understanding) (2001-11-5 17:12:16) Score 0 points
Too many methods:
GetAt, Left, Mid, Right, etc., it depends on how you get it!
Reply: dusb (Porsche) (2001-11-5 17:34:29) Score 0 points
But whether the return types of GetAt, Left, Mid, or Right are all CString, or they cannot be used, I want to get the string in it. Strangely, there is no string type in VC. (The string I want is to give the branch name in the tree control)
Reply: Alps_lou (Yun Feiyang) (2001-11-5 17:41:36) Score 0 points
There is a string type, and it needs to include <string>
Reply: luxes() (2001-11-5 17:42:19) Score 0 points
Add (LPCTSTR), which is equivalent to a const char *, and it cannot be used yet?
Reply: wt007(tt) (2001-11-5 17:48:33) Score 0 points
GetBuffer
Reply: espon99() (2001-11-5 17:54:06) Score 20 points
(LPSTR)(LPCTSTR)
Reply: ineedyou (Men of Ancient Temple) (2001-11-5 17:59:29) Score 0 points
...m_str.GetBuffer(needlen)...;
....
m_str.ReleaseBuffer()
Reply: dusb (Porsche) (2001-11-6 15:08:36) Score 0 points
The hero of espon99 is indeed a trick, but can you explain it?
19. How to convert from CString type to Unicode string type?
Reply: ychener (anemia) (2001-10-20 10:28:48) Score 0 points
CString itself supports Unicode.
As long as you choose UniCode to compile, the generated executable program supports UniCode.
Reply: ychener (anemia) (2001-10-20 10:30:04) Score 0 points
The CString class is adaptive just like TCHAR. If you define a UniCode macro, it will be compiled as UniCode.
Reply: xjl1980_81 (Along) (2001-10-20 10:35:16) Score 0 points
No, I have a function that requires Unicode string type, for example, L"abc" should be filled in, and the content in the quotes must be changed. Now I have a temp variable, which is of type CString, how to use it?
Reply: xt_jat(Samba) (2001-10-20 10:39:37) Score 0 points
_T()
_TEXT()
Is it OK?
Reply: xjl1980_81 (Along) (2001-10-20 10:43:18) Score 0 points
no
Reply: Jeffery__Chen() (2001-10-20 11:04:53) Score 0 points
Forced conversion:
CString temp;
WCHAR wTemp = (WCHAR)temp;
Reply: xjl1980_81 (Along) (2001-10-20 11:37:06) Score 0 points
to:Jeffery__Chen()
No, there is an error that cannot be converted
Reply: hongzhh (Guan'er) (2001-10-20 11:39:42) Score 0 points
The problem is this:
temp is a CString type variable with a value of zhh
Now there is an API
PCCERT_CONTEXT WINAPI CertFindCertificateInStore(
HCERTSTORE hCertStore,
DWORD dwCertEncodingType,
DWORD dwFindFlags,
DWORD dwFindType,
const void *pvFindPara, // Use L"zhh" here, no problem
//How to convert, can use temp
PCCERT_CONTEXT pPrevCertContext
);
Thank you everyone here, please help me
Reply: hongzhh (Guan'er) (2001-10-20 13:27:10) Score 0 points
WCHAR wszDomain[256];
MultiByteToWideChar( CP_ACP, 0, temp,
strlen(temp)+1, wszUserName,
sizeof(wszUserName)/sizeof(wszUserName[0]) );
wszUserName is the converted value
Reply: ychener (anemia) (2001-10-23 11:43:05) Score 0 points
As long as you use CString functions, if you want to use a function similar to strcpy, look at the functions that generally have responses in MSDN for Unicode. Just change to _tcscpy, etc.
Reply: ychener (anemia) (2001-10-23 11:44:10) Score 0 points
Have you defined Unicode macros?
20. How to convert the DATE data type read from the database back into a string in component development using ATL and does not support MFC?
Re-return: zhxuys(zhxuys) (2001-9-24 10:36:47) Score 0 points
ATL maps columns of datetime type to DBTIMESTAMP type, and can take out the year, month, day, etc. of this type, and then pass these data back to the client and use CTime to construct it on the client.
Reply: YUANXU (Xu) (2001-9-24 11:18:14) Score 0 points
to zhxuys:CTime is an MFC class and cannot be used when ATL does not support MFC. DATE is actually a double*
Reply: zhxuys(zhxuys) (2001-9-24 11:57:01) Score 0 points
You only use the data type specified by ATL or VARIANT type on the client and server side, but on the client, you can use MFC to reconstruct the desired data structure
twenty one. Type conversion, CString to wchar_t ??
CString ss("aabb");
wchar_t* cc;
cc=();
twenty two. How to convert CString type to _bstr_t type?
Reply: wei97081116 (Wei Xiaobao) (2001-9-4 11:19:30) Score 20 points
CString b;
_bstr_t a;
a=(_bstr_t)b;
Reply: zhaozhen1212 (Zhao Zhen) (2001-9-18 1:30:18) Score 0 points
_bstr_t a=();;
twenty three. How to convert a CString type into a normal string, such as char*?
Reply: liu_feng_fly (I hate the drivers, hey, just to make a living) (2001-9-17 18:00:52) Score 0 points
So, just use it directly, because there is such a conversion function in the class
Reply: ydogg (Gray-haired rabbit frequently) (2001-9-17 18:01:21) Score 0 points
CString show;
char *p = (());
Reply: jiangping_zhu (Nakulu's Wind Blade) (2001-9-17 18:02:05) Score 0 points
(char*)(LPCTSTR)str
Reply: bmouse (mouse) (2001-9-18 0:10:56) Score 0 points
Agree upstairs!
Reply: bmouse (mouse) (2001-9-18 0:13:22) Score 0 points
You can also use GetBuff to directly manipulate the CString buffer, but remember to free the buffer.
twenty four. Is the CString type converted to the unsigned char type? ?
Reply: LJN(*) is not as romantic as ever, and Yushu is standing against the wind (*) (2001-9-17 12:46:01) Score 0 points
Functions can be used
Reply: xpmao() (2001-9-17 13:09:09) Score 0 points
CString strWork;
MessageBox(0,(LPSTR)strWork,0,0);
or MessageBox(0,(0),0,0);
Reply: sandd (Dragon Subduing Palm) (2001-9-17 13:17:32) Score 0 points
CString string;
(LPCTSTR)string;
Reply: jeff_hunter(PandaLee) (2001-9-17 13:45:30) Score 0 points
(unsigned char *)(LPCTSTR)
Reply: fandh (Okay) (2001-9-17 14:00:57) Score 0 points
Just use (unsigned char *)(LPCTSTR)
Reply: ygd(ygd) (2001-9-17 16:11:17) Score 0 points
unsigned char *p;
CString str;
int length=();
for(int i=0;i<length;i++)
p[i]=(i);
Reply: Swordbroken (Scholar of Broken Sword) (2001-9-17 16:25:57) Score 0 points
CString str;
unsigned char string[30];
strcpy(string,str);
25. How to assign a value of an unsigned int type variable to a variable of type unsigned short and ensure that the value is not lost (of course the value is within a certain range)?
Reply: maxsuy (Magic Rabbit) (2001-8-14 16:37:30) Score 0 points
Just = OK
Reply: oppm(ppmm) (2001-8-14 16:38:11) Score 0 points
Direct assignment
Reply: milefo (Maitreya Buddha) (2001-8-14 16:40:40) Score 0 points
How can I lose the value if it is within a certain range?
unsigned short a;
unsigned int b;
a=( b & 0xffff);
You can try it!
26。CString ----char*
The variable of char* aa is defined. Now there is a CString variable bb. How to assign the value of bb to aa?
Reply: emmai(WaTaXiWaWaTaXi) (2001-8-10 11:57:33) Score 0 points
aa=();
Reply: hswqs(?????????????????????) (2001-8-10 11:59:01) Score 0 points
aa= (LPSTR)(LPCTSTR)bb;
Reply: ydogg (Gray-haired Rabbit) (2001-8-10 12:27:23) Score 0 points
=(());//The first method
= (LPSTR)(LPCTSTR)bb; //The second method
Reply: zhizhi() (2001-8-10 13:16:23) Score 0 points
aa= (char *)(LPCTSTR)bb,hehe
27. There is a BSTR* parameter in an interface function of a COM. You need to convert a char * type to a BSTR* type. Don't know how to convert it? Since you need to take out the parameter value after calling this function, you can only use pointers. In addition, what type of data should you use to pass parameters when calling? Prawns help.
BSTR bstr = SysAllocString(L"String");
When using this conversion, you use the address operator &, or the pointer must be new
Reply: yongyue2000i (Xiao Lu) (2001-9-9 18:38:26) Score 13 points
CString str = "abcd";
BSTR bstr = ();
Reply: houjzs() (2001-9-9 19:14:44) Score 13 points
BSTR b = SysAllocString(OLESTR("your string"));
28. Do type conversions need to be used to output a double number to the CEdit control?
Reply: jiangsheng (Jiang Sheng) (2001-8-24 14:46:17) Score 0 points
void AFXAPI DDX_Text( CDataExchange* pDX, int nIDC, double& value );
Reply: xiezhsh (Snow Walk) (2001-8-24 14:56:22) Score 0 points
If your CEDit-related member variable is Double type, it is not necessary at all. (In the dialog box where ClassWizard adds member variables, Variable Type can be selected as Double)
Reply: xiezhsh (Snow Walk) (2001-8-24 14:58:16) Score 0 points
If your CETdit-related member variable is not Double type, but CSTring type, then you need to use ltoa() to convert it to CSTring type.
Reply: Haven(Datian) (2001-8-24 14:58:32) Score 0 points
m_Edit.Fromat("%l",VarBouble);
updatedata(false);
Reply: 12345678() (2001-8-24 14:59:54) Score 0 points
CString m_Edit.Format("%lf", doubleVar);
GetDlgItem(EditID)->SetWindowText(m_strEdit);
29. How to convert WINDOWPLACEMENT * to char ** type??
(char**)&pWP
30. How to convert CString type to char* type?
Reply: dcz(dcz) (2001-8-19 19:13:27) Score 5 points
// str is CString var
char* temp = strdup(str);
...
free(temp);
Reply: yu900 (Wolf of the Wind) (2001-8-19 19:57:25) Score 0 points
getbuffer(); just!
Reply: aileen_long (Challenge 2001) (2001-8-19 21:10:35) Score 0 points
Agree with the opinion upstairs!
Reply: czh912() (2001-8-19 21:27:08) Score 0 points
char buf[20];
printf(buf,"%s",string);
Reply: casl(casl) (2001-8-19 22:59:44) Score 5 points
CString s("abc");
char* temp=(10);
...
();
Reply: cocia (Gao Ya) (2001-8-19 23:04:23) Score 0 points
char* temp=(10);
What does 10 mean
Reply: kevin_dong (Dream Immortal) (2001-8-20 10:26:35) Score 0 points
// str is CString var
char* temp = strdup(str);
// free
free(temp);
My code can be compiled in one program, but in another, there is always an error of not convert parameter 1 from 'class CString' to 'const char *'. The types of str and temp are the same. why is that?
Reply: dcz(dcz) (2001-8-20 14:13:45) Score 0 points
you may setting your compiler option to UNICODE, in this case, declare the var:
// str is CString var
_TCHAR* temp = _tcsdup(str);
// free
free(str);
31. SA and SB are two structural types??
SA* A;
SB* B;
(SB*)A->... (Calling function)
Is the type of A at this time pointing to SA or SB
Is the compiler generating a temporary pointer at this time?
in addition,
B=(SB*)A; What type is A at this time? ? ?
Reply to post:
Reply: ddeng (Dengdeng) (2001-8-9 17:13:58) Score 0 points
A's type is always SA*
Type B is always SB*
When casting, it is a temporary pointer
Reply: gold_water (no resistance to wind and rain) (2001-8-9 17:30:46) Score 0 points
Agree upstairs.
32. char buff[100], char UserName[50][100], how to pass the value of buff to UserName, which is an item in the UserName array? ?
//0=<i<50
strcpy(UserName[i],buff);
Reply: Ashura (Ashura) (2001-7-26 10:08:20) Score 0 points
Hoho, benbensan takes the lead.
Reply: tuita (Matador) (2001-7-26 10:13:22) Score 0 points
for (i=0;i<100;i++)
*(*(username+x)+i)=*(buffer+i)
Among them 0 "X<50
Benbensan also wrote it right
Reply: kekeke (I am here to learn from everyone) (2001-7-26 10:24:22) Score 0 points
What about the other way around? What if you assign a certain item in UserName to a buff? How to do it?
Reply: benbensan (An An An An An) (2001-7-26 10:26:53) Score 0 points
//0=<i<50
strcpy(UserName[i],buff);
Reply: benbensan (An An An An An) (2001-7-26 10:28:15) Score 0 points
Sorry, you can be wrong, but it is recommended that you look at the pointers and arrays in C language
//0=<i<50
strcpy(buff,UserName[i]);
Reply: jfzsl (Commander-in-Chief of the Bandit Suppression) (2001-7-26 10:32:57) Score 0 points
Let’s take a good look at Lao Tan’s books first! OK?
Reply: kekeke (I am here to learn from everyone) (2001-7-26 10:44:25) Score 0 points
good. . . . !
33. How to convert SYSTEMTIME type to time_t type?
SYSTEMTIME st;
GetLocalTime(&st);
CTime tm(,,,,,);
time_t t = ();
34。unsigned char Exponent[10]; //e
unsigned long eBytes; // the number of bytes of e
How to convert to DWord type! ??
Use cast type
(DWord)eBeytes;
(DWord)Exponent[i];//(0<=i<=10);
Reply: xjl1980_81 (Along) (2001-7-26 16:47:29) Score 0 points
I mean converting e to DWORD type
That is to say, convert the content in Exponent into DWORD type
Reply: cloudshadow1 (Yunshadow) (2001-7-26 17:13:30) Score 0 points
Just use cast type conversion, (DWORD's higher 24-bit automatically adds 0)
DWORD Des[10]
for (int i=0;i<11;i++)
Des[i]=Exponent[i];
As for the ULONG, it's fine to use the forced type to soft change.
35. How to convert the time_t type to SYSTEMTIME type?
Reply: Haven(day by day) (2001-7-26 17:12:36) Score 0 points
typedef struct _SYSTEMTIME
typedef long time_t
Obviously not possible!
Reply: facexy (FACE) (2001-7-26 17:17:38) Score 0 points
Oh, I asked the wrong question, the object before and after is the opposite;-(
The result of being busy......
Also, TO upstairs:
The conversion is OK
struct tm *tblock;
SYSTEMTIME SystemTime;
memset(&SystemTime,0,sizeof(SYSTEMTIME));
tblock=localtime(&timer);
=tblock->tm_year+1900;
=tblock->tm_mon+1;
=tblock->tm_mday;
=tblock->tm_hour;
=tblock->tm_min;
=tblock->tm_sec;
=tblock->tm_wday;
return &SystemTime;
Reply: zjh73 (big octopus) (2001-7-26 20:28:28) Score 0 points
There are two ways:
1. Use CTime class
First use the time_t type to construct a CTime object, then define a SYSTEMTIME structure, and finally use the member function GetAsSystemTime of the CTime class to convert time to the SYSTEMTIME structure.
Just do it.
2. Use gmtime function
gmtime function converts time_t time into the tm structure and returns a tm pointer, and then assigns the corresponding term of the tm structure to the corresponding term of the SYSTEMTIME, but you need to note that using this method
I mean, the two structures have some differences in the counting methods of days, weeks, etc. One generally starts from 0, and the other generally starts from 1. Pay attention to correction when assigning values, and also pay attention to SYSTEMTIME
There is one in the structure that is milliseconds, and time_t is counted in seconds.
Reply: zjh73 (big octopus) (2001-7-26 20:32:13) Score 0 points
In turn, you can use the Ctime class method
It is to first use the SYSTEMTIME structure to construct a CTime object, and use the member function GetTime in the CTime class to return a corresponding time_t. 36. I am learning SDK programming now, and the problem I am having is:
I defined a static long shaping variable,
static long lScore=0;
I want to replace the title of the window with a long plastic digit, and use the SetWindowText function to implement it.
Since its second parameter requires the data type to be unsigned short *, but it is used to implement casting
There is always a compilation error:
cannot convert parameter 2 from 'unsigned short *' to 'const char *'
Later, it was changed to LPCTSTR to implement casting, and there was no compilation error, but the function was always executed unsuccessfully.
I would like to ask you experts, what's going on? ? ?
Reply to post:
Reply: prog_st(st) (2001-8-4 21:20:07) Score 0 points
/* : This program converts integers of various
* sizes to strings in various radixes.
*/
#include <>
#include <>
void main( void )
{
char buffer[20];
int i = 3445;
long l = -344115L;
unsigned long ul = 1234567890UL;
_itoa( i, buffer, 10 );
printf( "String of integer %d (radix 10): %s/n", i, buffer );
_itoa( i, buffer, 16 );
printf( "String of integer %d (radix 16): 0x%s/n", i, buffer );
_itoa( i, buffer, 2 );
printf( "String of integer %d (radix 2): %s/n", i, buffer );
_ltoa( l, buffer, 16 );
printf( "String of long int %ld (radix 16): 0x%s/n", l,
buffer );
_ultoa( ul, buffer, 16 );
printf( "String of unsigned long %lu (radix 16): 0x%s/n", ul,
buffer );
}
Output
String of integer 3445 (radix 10): 3445
String of integer 3445 (radix 16): 0xd75
String of integer 3445 (radix 2): 110101110101
String of long int -344115 (radix 16): 0xfffabfcd
String of unsigned long 1234567890 (radix 16): 0x499602d2
Reply: lwg7603 (released person after *) (2001-8-4 21:36:15) Score 0 points
TCHAR str[255]={_T('/0')};
_stprintf(str,_T("%d"),lScore);
SetWindowText(hwnd,str);
37. What I need in the middle of the buf I send with socket is char* data, and I want to convert a struct directly into char* and send it to it.
I use
struct ABCD *abcd;
char *buf;
abcd = (ABCD *)calloc(1,sizeof(ABCD));
buf = (char *)calloc(1,sizeof(ABCD));
///
Assign a value to the middle of abcd, which has multiple values of char[] and values of int
///
memcpy(buf,abcd,sizeof(ABCD));
//strcpy(buf,(char *)abcd); also not
sock(host,buf,....);
//sock(host,(char *)buf,...); also not
The problem is that the value in the middle of this buf is always wrong, everyone knows why no.
Reply: wolf721() (2001-7-30 18:18:34) Score 5 points
You are passing a pointer value, not data
Reply: kiko_lee (look around) (2001-7-30 18:50:49) Score 0 points
But using memcpy is to copy the entire data in the past
Reply: lz_0618(lz_0618) (2001-7-30 19:26:44) Score 5 points
The VC you used is changed to ABCD *abcd; there is no problem with the compilation after it is compiled!
sock(host,buf,......);I don't know what this is, custom function?
typedef struct _ABCD
{
int ID;
char Name[10];
}ABCD;
.......
ABCD *abcd;
char *buf;
abcd = (ABCD *)calloc(2,sizeof(ABCD));
buf = (char *)calloc(2,sizeof(ABCD));
///
// Assign a value to the middle of abcd, which has multiple values of char[] and values of int
abcd[0].ID =1;
abcd[1].ID =2;
///
memcpy(buf,abcd,2*sizeof(ABCD));
strcpy(buf,(char *)abcd);//Not
The content in the buf is also correct! !
Reply: kiko_lee (look around) (2001-7-31 8:57:52) Score 0 points
I did it as my brother upstairs said, but I still couldn't do it. I used
memcpy(buf,abcd,sizeof(ABCD));
The middle abcd, I don’t know if it’s a problem with the address.
Reply: supersusheng (Xiao Su) (2001-7-31 14:30:42) Score 0 points
Boss, let’s see how big the value you get from sizeof() is.
Reply: ydogg (Gray-haired Rabbit) (2001-7-31 14:41:52) Score 0 points
Only stream data can be passed, and the structure cannot be passed.
Reply: IamNotMan(NorGirl) (2001-7-31 14:50:53) Score 5 points
I use this often
ABCD a ;
// Assign values to each domain of a (must not contain pointer items)
char* buff = new char[sizeof(ABCD)];
memcpy(buff,&a,sizeof(ABCD));
//or *(ABCD*)buff = a;
.................
If the number in the buff is correct, it means the problem is not here.
Reply: zb_china (the last water truck zb_china Sina) (2001-7-31 15:16:24) Score 0 points
Can't understand
Reply: eggplant (Lala) (2001-7-31 15:42:48) Score 0 points
It is best to use memcpy(), because the value in struct may contain zero bytes, so strcpy() may be wrong. If you pass struct, it is best to change the byte alignment of struct to bytes.
Reply: lvfengxun(lfx) (2001-7-31 16:06:57) Score 5 points
Just send the structure pointer as the send parameter, what else can you convert
Is it necessary to discuss it here?
struct AA
{
int a;
char b[100];
};
struct AA aa;
=11;
strcpy(,"aaa");
send(hSocket,(char *)(&aa),sizeof(aa),0);
//OK
Reply: mydewang(mydewang) (2001-7-31 16:33:21) Score 0 points
Actually, this is a byte alignment problem.
for example
struct AA
{
int a;
char b;
};
Then sizeof( struct AA ) does not equal 5, but 8. Therefore, if you assign this structure to a char *, there will be some more zeros in it...
If you need to solve this problem, you can add /Zp1 to Project->Setting->Link->Project Options
In addition, you can refer to the compilation options of /Zp in MSDN...
Reply: lz_0618(lz_0618) (2001-7-31 19:43:54) Score 0 points
It's not a problem with byte alignment. The program above is easy to use after testing. What is the error of this guy using my program? Can you explain it clearly? ? ?
I have no problem using socket to send the structure, and it is communicating between programs compiled by VC and Delphi. Of course, you should pay attention to the problem of byte alignment at this time. In VC programs,
As long as the server and client's compilation environment is not deliberately set up differently, there will definitely be no problem. At most, you can just pass on a few more Bits.
Reply: kiko_lee (look around) (2001-8-3 11:02:51) Score 0 points
I found that there are some problems with putting char * memcpy into char * middle, but if you put it in the middle of char [], do you know why?
Reply: ydogg (Gray-haired Rabbit) (2001-8-3 11:40:35) Score 0 points
memcpy does not copy the last '/0'...
Reply: wenjunlin2000 (Microsoft Nemesis) (2001-8-3 14:32:17) Score 0 points
You read it wrong
Because char* ends with 0
Reply: mc_music (Single opening) (2001-8-3 15:07:21) Score 0 points
Please note my program:
struct ABCD *abcd;
char *buf;
abcd = (ABCD *)calloc(1,sizeof(ABCD));
//Initialize abcd
buf=abcd;// Just pointer value
Reply: zhangnanonnet (WinSockZhang) (2001-8-3 16:21:03) Score 0 points
Try changing the type to BYTE
Reply: kiko_lee (look around) (2001-8-7 9:21:08) Score 0 points
No matter what, everyone has a lot of suggestions for giving points.
38. double dou=12.34;How can I get char * ch="12.34";What is the conversion function?
Reply: wyzegg(egg) (2001-7-24 21:26:04) Score 50 points
double dou=12.34;
char * ch;
ch=malloc(100);
sprintf(ch,"%5.2f",dou);
Reply: wyzegg(egg) (2001-7-24 21:28:24) Score 0 points
or
#include <>
#include <>
void main( void )
{
int decimal, sign;
char *buffer;
int precision = 10;
double source = 3.1415926535;
buffer = _ecvt( source, precision, &decimal, &sign );
printf( "source: %2.10f buffer: '%s' decimal: %d sign: %d/n",
source, buffer, decimal, sign );
}
But the first one is commonly used
Reply: Matrix_w (learning a little bit) (2001-7-24 21:32:43) Score 30 points
int decimal, sign;
double dou =12.34;
char* ch;
ch = _ecvt(dou,4,&decimal,&sign);
Reply: imhua (Hua Di) (2001-7-24 21:35:02) Score 20 points
double dou=12.34;
char *str;
gcvt(dou,5,str); //5 is the length
MessageBox(str);
Reply: Matrix_w (learning a little bit) (2001-7-24 21:37:58) Score 0 points
/* _GCVT.C: This program converts -3.1415e5
* to its string representation.
*/
#include <>
#include <>
void main( void )
{
char buffer[50];
double source = -3.1415e5;
_gcvt( source, 7, buffer );
printf( "source: %f buffer: '%s'/n", source, buffer );
_gcvt( source, 7, buffer );
printf( "source: %e buffer: '%s'/n", source, buffer );
}
Output
source: -314150.000000 buffer: '-314150.'
source: -3.141500e+005 buffer: '-314150.'
39. I call a stored procedure in ADO. The stored procedure has three input parameters @useradd char(30), @username char(10), @userage char(3). Now I want to put char
*addr,char *name,char *age are assigned to them separately. ??
I made the following definition:
_ParameterPtr para1;
_variant_t var1,var2,var3;
==============================================================
=VT_BSTR;
=addr;/////(Compilation error)
==============================================================
para1=m_pCommand->CreateParameter(L"useradd",adBSTR,adParamInput,30,var1);
m_pCommand->Parameters->Append(para1);
The following error occurred in the compilation result:
cannot convert from 'char *' to 'unsigned short *
I don't know if the type is to choose VT_BSTR?
Reply: tar(GPS) (2001-7-12 18:49:05) Score 15 points
A string that should point to a wide character,
unicode string
use _bstr_t to convert it
_bstr_t var1(addr);
Reply: happyhackwang() (2001-7-12 20:06:48) Score 5 points
char *To be converted to BSTR
Reply: WhiteWaterBlueSky (Crazy Digital) (2001-7-13 9:35:15) Score 10 points
This is what it is in the SDK
1. First use MultiByteToWideChar to convert char* to wchar_t*
2. Then use SysAllocString to convert wchar_t* to BSTR
Reply: tar(GPS) (2001-7-13 14:10:01) Score 0 points
Faint, I've written the sentences
_bstr_t a(addr);
=VT_BSTR;
=(wchar_t *)a;
Reply: xwchena (God of the West Wind) (2001-7-13 15:57:00) Score 0 points
Brother, after I finished modifying the record collection, I returned without results. My code looks like this:
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset=m_pCommand->Execute(NULL,NULL,adCmdStoredProc);
if(!m_pRecordset->adoEOF)
{
result1=m_pRecordset->GetCollect((long)0);
if(!=NULL)
{
(VT_BSTR);
CString strResult1=;
strcpy(resval,strResult1);
}
During debugging, we saw that after executing m_pCommand->Execute(), m_pRecordset reaches the end of the record set.
If the char* type is changed to int type, the result can be returned to the record set.
Why is this? ? ?
Reply: tar(GPS) (2001-7-13 19:19:51) Score 0 points
try adVarChar
Reply: xwchena (God of the West Wind) (2001-7-13 22:54:21) Score 0 points
Still no results are returned in the record set
Reply: seesi (It’s not that I want to lie to you, it’s that I don’t know how to avoid lie!) (2001-7-14 0:24:36)
40. How to assign BSTR pVal to: unsigned char *pw; How to assign: unsigned char digest[16] to BSTR *pOutVal?
USES_CONVERSION;
*pOutVal=SysAllocString(A2W((LPTSTR)digest));
unsigned char *pw = (unsigned char *)_com_util::ConvertBSTRToString(pInVal);