Click here to Skip to main content
15,887,596 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralOff topic Pin
Richard MacCutchan2-Oct-12 2:55
mveRichard MacCutchan2-Oct-12 2:55 
GeneralRe: Off topic Pin
«_Superman_»2-Oct-12 3:00
professional«_Superman_»2-Oct-12 3:00 
GeneralRe: Off topic Pin
Richard MacCutchan2-Oct-12 3:23
mveRichard MacCutchan2-Oct-12 3:23 
QuestionTooltip for ATL toolbar Pin
Sakhalean24-Sep-12 18:43
Sakhalean24-Sep-12 18:43 
QuestionCAsyncSocket Send( arguments ) Pin
bkelly1318-Sep-12 16:13
bkelly1318-Sep-12 16:13 
AnswerRe: CAsyncSocket Send( arguments ) Pin
Richard MacCutchan18-Sep-12 21:50
mveRichard MacCutchan18-Sep-12 21:50 
GeneralRe: CAsyncSocket Send( arguments ) Pin
bkelly1319-Sep-12 12:13
bkelly1319-Sep-12 12:13 
AnswerRe: CAsyncSocket Send( arguments ) Pin
pasztorpisti29-Sep-12 23:52
pasztorpisti29-Sep-12 23:52 
Here is the thing: In C/C++ every pointer is automatically casted into (const void*) and every non-const pointer is automatically casted into (void*). The msdn web page used cast just because of the pointer arithmetic involved, as you see they add an integer value to the start of the buffer: (LPCTSTR)m_sendBuffer + m_nBytesSent. They cast the buffer pointer into LPCTSTR because they want the addition to step the pointer with *byte* granularity. The problem with this is that LPCTSTR can translate not only to (const char*) but also into (const wchar_t*) (when the project character set is unicode, and note that wchar_t is a type whose size is 2 bytes!) so I guess MS guys made a mistake here and (LPCTSTR) is a bug (if the project character setting is set the unicode), they should have used (char*) or its equivalent in winodws: LPSTR or const char* or LPCSTR or something like that, something that is a *byte* pointer. Your problem is that you try to convert an instance of your struct into a pointer. You can not convert an instance into a pointer! You can converty only a pointer into a different type of pointer and in rare cases conversion might be needed between pointer and integral types.
C++
SYSTEMTIME m_current_time;
GetSystemTime( &m_current_time );
int t1 = sizeof( m_current_time );
// This is the case where you try to convert your instance into a pointer incorrectly:
int chars_sent = m_C_Server_Send_Time_Socket->Send( (LPCTSTR) m_current_time, t1,   0 );
// Here is the correct way to do that:
int chars_sent = m_C_Server_Send_Time_Socket->Send( (LPCTSTR) &m_current_time, t1,   0 );
// Note that every pointer can be converted into (const void*)
//  so the cast is totally unnecessary and you can write simply:
int chars_sent = m_C_Server_Send_Time_Socket->Send( &m_current_time, t1, 0 );

Note that using (LPCTSTR) is a bug even on microsoft's side! You need to convert your struct pointer into a (char*) only if you want to step your pointer with byte precision!
C++
SYSTEMTIME m_current_time;

// The following steps the pointer with sizeof(SYSTEMTIME) bytes in memory!!!!!
// The resulting pointer points to the first byte that follows the last byte of your struct.
SYSTEMTIME* p = &m_current_time + 1;

//Since pointers and arrays in C/C++ work very similarly the above code is identical to this:
SYSTEMTIME* p = &(&m_current_time)[1];
//Pointer arithmetic and array indexing behave very similarly.

// The following expressions step the pointer just by 1 byte (sizeof(char)) in memory!!!!!
// We basically index into our struct as if it was a byte array...
// This is what MS guys wanted to do but in some cases (with unicode character setting)
// their code steps the pointer with 2 byte granularity (sizeof(whcar_t)) that is a bug.
char* p = (char*)&m_current_time + 1;

// The statements below are also valid because <code>char*</code> (like any other non-const
// pointer) is automatically casted to both <code>void*</code> and <code>const void*</code>.
void* p = (char*)&m_current_time + 1;
const void* p = (char*)&m_current_time + 1;

Note that an addition or a substraction on a pointer always steps the pointer with the size of the type the pointer points to (like when you indexing into an array of the specified type). For this reason you can not step void pointers without casting them into something else - the size of void isn't defined.
GeneralRe: CAsyncSocket Send( arguments ) Pin
Richard MacCutchan30-Sep-12 1:07
mveRichard MacCutchan30-Sep-12 1:07 
GeneralRe: CAsyncSocket Send( arguments ) Pin
pasztorpisti30-Sep-12 3:16
pasztorpisti30-Sep-12 3:16 
GeneralRe: CAsyncSocket Send( arguments ) Pin
Richard MacCutchan30-Sep-12 4:00
mveRichard MacCutchan30-Sep-12 4:00 
GeneralRe: CAsyncSocket Send( arguments ) Pin
pasztorpisti30-Sep-12 4:07
pasztorpisti30-Sep-12 4:07 
GeneralRe: CAsyncSocket Send( arguments ) Pin
pasztorpisti30-Sep-12 4:03
pasztorpisti30-Sep-12 4:03 
GeneralRe: CAsyncSocket Send( arguments ) Pin
Richard MacCutchan30-Sep-12 4:13
mveRichard MacCutchan30-Sep-12 4:13 
GeneralRe: CAsyncSocket Send( arguments ) Pin
bkelly1330-Sep-12 5:44
bkelly1330-Sep-12 5:44 
GeneralRe: CAsyncSocket Send( arguments ) Pin
Richard MacCutchan30-Sep-12 6:10
mveRichard MacCutchan30-Sep-12 6:10 
GeneralRe: CAsyncSocket Send( arguments ) Pin
pasztorpisti30-Sep-12 6:38
pasztorpisti30-Sep-12 6:38 
GeneralRe: CAsyncSocket Send( arguments ) Pin
bkelly1330-Sep-12 6:16
bkelly1330-Sep-12 6:16 
GeneralRe: CAsyncSocket Send( arguments ) Pin
pasztorpisti30-Sep-12 6:31
pasztorpisti30-Sep-12 6:31 
Questionget instance of object (in exe) from dll caller Pin
MrKBA17-Sep-12 4:38
MrKBA17-Sep-12 4:38 
AnswerRe: get instance of object (in exe) from dll caller Pin
pasztorpisti17-Sep-12 6:57
pasztorpisti17-Sep-12 6:57 
GeneralRe: get instance of object (in exe) from dll caller Pin
MrKBA17-Sep-12 9:32
MrKBA17-Sep-12 9:32 
JokeRe: get instance of object (in exe) from dll caller Pin
pasztorpisti17-Sep-12 13:43
pasztorpisti17-Sep-12 13:43 
GeneralRe: get instance of object (in exe) from dll caller Pin
pasztorpisti17-Sep-12 13:48
pasztorpisti17-Sep-12 13:48 
AnswerRe: get instance of object (in exe) from dll caller Pin
Richard MacCutchan17-Sep-12 22:16
mveRichard MacCutchan17-Sep-12 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.