Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created a data structure as follows : -
C#
typedef struct Entity {
    int Id;
    int Type;
    int Size;
    int ParentId;
    int Position;
    int Length;
    int NeteorkTraffic;
    CxyPoint Location;
    wchar_t System[1];
    wchar_t ConnectionList[20];
    wchar_t Caption[20];
}stEntity;


When i use the following code : -
C#
CString strData;
pComboBox1->GetWindowTextW(strData.GetBuffer(10),10);
wcscpy(objEntity.System,L"A");
pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CONNECTION);
pEdit->GetWindowTextW(strData);
// On Following Line Value Of objEntity.System again updated with wrong value automatically. 
wcscpy(objEntity.ConnectionList,strData);


It also appends the System variable with ConnectionList values.
Copy Of Cstring to wchar_t is not done correctly.

Please help me to slove this issue.
Posted

your System member is only one char wide, so there's no room for the terminating NULL ... all the string functions use the presence of a null to determine the end of the string they're working on - in this case they find the first null in ConnectionList
 
Share this answer
 
Comments
Espen Harlinn 19-May-12 6:12am    
Well answered :-D
So when you do something like...
C++
MessageBox( NULL, &system, L"The system is", MB_OK );

you get what's in System displayed with the contents of ConnectionList?

If that's the case it's because System runs into ConnectionList. When MessageBox (or whatever uses System) expects System to be a NULL terminated string and it's not, it's just one character and the next terminator in memory is at the end of ConnectionList.

So what can you do about it? First thing is stop using functions that expect NULL terminated strings to manipulate stuff. It's never going to work with your structure. Secondly stop using wscpy, even C programmers avoid it as it's so easy to mess up with and go stomping all over memory you shouldn't. And if you really have to call a function that expects a constant zero terminated string for System buffer it:
C++
std::wstring window_text_buffer( 1, objEntity );
MessageBox( NULL, text_buffer.c_str(), "The system is", MB_OK );

Final thing is start being a C++ programmer. I know this sounds harsh but all the time you're using C functions and casts your heart's not really in it. Or you're stuck in a early 1990s timwarp. Stop using structures that don't have any behaviour (occasionally you might have need of a data aggregate, but it's not often), investigate the C++ equivalents of what you're doing with C functions and stop using C style casts.
 
Share this answer
 
maby ( strData ) String length is bigest than ( jEntity.ConnectionList ) length , begining show all buffers and strings in MessageBox to see what show and then do this works ,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900