|
toxcct wrote:
Run-Time Check Failure #2 - Stack around the variable 'EditorOptions' was corrupted.
This means that some variable on your stack was overflowed or underflowed. Also that the program asserts when you leave the handler indicates that that the stack is corrupted in that function, not in the DoModal .
toxcct wrote:
::strcpy(m_strEditorVersion, EditorVersion);
I think that the problem is here. You call un unsafe C function on a variable with an uninitialized length! If my assumtions are correct, m_strEditorVersion is of type CString .
If I'm correct, see what the following gets you:
::strncpy(m_strEditorVersion.GetBufferSetLength(256), EditorVersion, 255);
m_strEditorVersion.ReleaseBuffer();
Your problem was because you were writing to a CString buffer that had a size which was too small. A better solution would be:
COptionsDlg::COptionsDlg(TEditorOptions& EditorOptions, <br />
CString EditorVersion,<br />
CWnd* pParent )<br />
: CDialog(COptionsDlg::IDD, pParent),<br />
m_EditorOptions(EditorOptions),
m_strEditorVersion(EditorVersion) <br />
{
m_strDigitsInYears.Format("%d", m_EditorOptions._NbDigitsInYears); <br />
m_strDigitsInPhones.Format("%d", m_EditorOptions._NbDigitsInPhones);<br />
}<br />
May I ask why you use ::strcpy to copy CString contents?
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Bob Stanneveld wrote:
m_strEditorVersion(EditorVersion)
{ // or just try:
// m_strEditorVersion = EditorVersion;
in fact, i did this first, but the compiler told me to put this in the constructor initialization list...
Bob Stanneveld wrote:
May I ask why you use ::strcpy to copy CString contents?
i do ::strcpy(m_strEditorVersion, EditorVersion); because m_strEditorVersion is defined as a char[10] ...
so, the code you suggest me with ReleaseBuffer() won't be able to work...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
toxcct wrote:
i do ::strcpy(m_strEditorVersion, EditorVersion); because m_strEditorVersion is defined as a char[10]...
so, the code you suggest me with ReleaseBuffer() won't be able to work...
Your naming convention is a little bit misleading. m_strEditorVersion suggests that is is a normal string instead of a C-Style string. Use m_pszEditorVersion instead.
Back to the problem. Did you check the length of the EditorVersion string? You are just copying the string without length checking. This is a horrible mistake! You should use ::strncpy and copy up to 10 characters if you don't care about the '\0' or only 9 and set the NULL-terminator yourself. I'm almost 100% sure that this solves your problem.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
eBob Stanneveld wrote:
Did you check the length of the EditorVersion string? You are just copying the string without length checking
currently, yes you're right, but as i use the debugger to confirm, m_strEditorVersion is a char[10] and contains only "2.10" (correctly terminated with its trailing '\0' character)...
well, i'll test the changes on the afternoon, but now, i dont have enough time to tell you if the changes are useful or not. however, i think i'll change the char[10] to a CString, regardless to the memory occupied...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Do you still get the error with the range checking code?
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
hi bob, i'm back
i've edited my first post on this thread... the error remains the same, even when i changed the m_strEditorVersion into CString...
i think it is not the source of the problem because the Run-Time talks about TEditorOtions ...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Hey,
toxcct wrote:
i've edited my first post on this thread... the error remains the same, even when i changed the m_strEditorVersion into CString...
At least that will not cause any problems in the future anymore.
toxcct wrote:
i think it is not the source of the problem because the Run-Time talks about TEditorOtions...
I cannot see what the problem is because I don't know your code, but you could set a memory breakpoint at the address of the variable that gets overwritten. Your debugger will do its trick at the moment the memory is changed.
See here[^] and here[^] for more information about memory breakpoints.
Good luck!
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Want to uninstall a U disk by VC++ program under windows2000
how to coding the program? have the api function about this?
|
|
|
|
|
What is a "U disk?"
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I have created a simple sitch(), it takes integers
int choice;
switch(choice)
case 1:
break
.
.
default
message
break;
this works fine as long as the user enters a number (integers) but if I enter leters a,b,XD ..etc
it goes out in a loop.
how to chakec data type to ensure it is 1-9, if not display a message
Any way would be good
thanx
|
|
|
|
|
Do this before your switch statement:
It just checks if the values are in the specified range.
if (choice >= 48 && choice <= 57)
{
switch (choice)
...
}
this is this.
|
|
|
|
|
Anonymous wrote:
how to chakec data type to ensure it is 1-9, if not display a message
Your switch does this automatically with the default block. What else do you want?
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Don't think you read the question right. The default works fine if an interger is entered (any number) but if a character is enter, it won't work!!!!!
|
|
|
|
|
Anonymous wrote:
ForumVisual C++
Subject:Re: switch() function
Sender:Anonymous
Date:14:18 7 Jul '05
Don't think you read the question right. The default works fine if an interger is entered (any number) but if a character is enter, it won't work!!!!!
I don't think that you can program very well. That's OK since we are here to help.
I read the question and I can assure you that I understand it perfectly well! Your problem is that you don't know what you are doing. Let me elaborate: characters (I'll stick to single byte characters for simplicity) are represented by integral values in the range 0x0 to 0xFF. Integers (the usual 32bit integers) have values in the range 0x0 to 0xFFFFFFFF. What did we learn from this is that you can store an character into an int without problems. So a simple static_cast will solve your problem.
If you work with the datatype char however, the following code will do the trick:
std::cin >> cYourChar;
switch( cYourChar )
{
case '0': { break; }
case '1': { break; }
case '2': { break; }
case '3': { break; }
case '4': { break; }
case '5': { break; }
case '6': { break; }
case '7': { break; }
case '8': { break; }
case '9': { break; }
default:
}
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
as above, the exception will be caught in the "default" case.
it's not necessarilly the fault of the switch but of the input.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
hi all
I want to use mfc in win32 dll. but this error happens.how to deal with this error?
fatal error C1189: #error :WINDOWS.H already included. MFC apps must not #include <windows.h>
I love you not for whom you are, but who i am when I'm by your side
|
|
|
|
|
gohappy_1999 wrote:
I want to use mfc in win32 dll.
Thats not possible, as MFC is Upgrade/wrapper of Win32 api's. So according to Windows Programming Thumb Rule, Any upgraded thing will not be supported in condition prior to upgrade.
So,In order to use MFC in dll,You have to create MFC based DLL.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
I have done some work recently in C#, integrating the product I work on with another project that exposes a COM object for this purpose. Now a third party has asked me to do the same in C++. This product in C# took an array of objects as a parameter, and returned an array of objects. The objects were strings. In C++, I've done this:
const char * pClientFields[11] = {"CLIENT_RECD",<br />
"CLIENT_FIRST",<br />
"CLIENT_LAST",<br />
"CLIENT_ADDRESS",<br />
"CLIENT_CITY",<br />
"CLIENT_STATE",<br />
"CLIENT_ZIP",<br />
"CLIENT_AREA",<br />
"CLIENT_PHONE",<br />
"CLIENT_BUSINESS",<br />
"CLIENT_EMAIL"};<br />
<br />
_variant_t * ClientFields = NULL;<br />
<br />
_variant_t * GetClientFields()<br />
{<br />
if (ClientFields == NULL)<br />
{<br />
CComSafeArray<BSTR> array;<br />
for(int i=0; i < 11; ++i)<br />
{<br />
array.Add(_bstr_t(pClientFields[i]));<br />
}<br />
<br />
VARIANT * pVar = new VARIANT();<br />
pVar->vt = VT_ARRAY | VT_BSTR;<br />
pVar->parray = array.Detach();<br />
<br />
ClientFields = new _variant_t(pVar);<br />
}<br />
<br />
return ClientFields;<br />
}
because the COM interface is expecting a _variant_t( and it returns one, too ). ClientFields at the end of this does not know what sort of variant it is, and so I get an exception. Can anyone please show me what I need to do here to get a _variant_t full of strings, and also how to read one at the other end ? I need it to compile in VC6 ( although the above is written in VC7 )
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
Can anyone please show me what I need to do here to get a _variant_t full of strings, and also how to read one at the other end ?
You can create the variant with something like this:
void BstrArrayFromWcharArray(VARIANT& var, WCHAR** pStrings, int size)
{
var.vt = VT_ARRAY | VT_BSTR;
var.parray = SafeArrayCreateVector(VT_BSTR, 0, size);
for (long ix = 0; ix < size; ix++)
SafeArrayPutElement( var.parray, &ix, ::SysAllocString(pStrings[ix]) );
}
WCHAR* pClientFields[] =
{
L"CLIENT_RECD",
L"CLIENT_FIRST",
L"CLIENT_LAST",
L"CLIENT_ADDRESS",
L"CLIENT_CITY",
L"CLIENT_STATE",
L"CLIENT_ZIP",
L"CLIENT_AREA",
L"CLIENT_PHONE",
L"CLIENT_BUSINESS",
L"CLIENT_EMAIL"
};
_variant_t ClientFields;
_variant_t* GetClientFields()
{
if (ClientFields.vt == VT_EMPTY)
{
UINT size = sizeof(pClientFields)/sizeof(*pClientFields);
BstrArrayFromWcharArray(ClientFields, pClientFields, size);
}
return &ClientFields;
}
You can access the elements of the array inside the variant with something like this:
void ShowBstrArray(const VARIANT var)
{
if (var.vt != (VT_ARRAY | VT_BSTR))
return;
SAFEARRAY* psa = var.parray;
for (long i = 0; i < (long)psa->rgsabound[0].cElements; i++)
{
BSTR bstr;
SafeArrayGetElement(psa, &i, &bstr);
CString s = bstr;
AfxMessageBox(s);
}
}
void Test()
{
_variant_t* pFields = GetClientFields();
ShowBstrArray(*pFields);
}
However, when accessing most or all of the elements in the array, the following alternatives, which make use of SafeArrayAccessData , are more efficient:
void BstrArrayFromWcharArray2(VARIANT& var, WCHAR** pStrings, UINT size)
{
var.vt = VT_ARRAY | VT_BSTR;
var.parray = SafeArrayCreateVector(VT_BSTR, 0, size);
BSTR HUGEP* pbstr = NULL;
if (FAILED(SafeArrayAccessData(var.parray, (void HUGEP**)&pbstr)))
return;
for (UINT ix = 0; ix < size; ix++)
pbstr[ix] = ::SysAllocString(pStrings[ix]);
SafeArrayUnaccessData(var.parray);
}
void ShowBstrArray2(const VARIANT var)
{
if (var.vt != (VT_ARRAY | VT_BSTR))
return;
SAFEARRAY* psa = var.parray;
BSTR HUGEP* pbstr = NULL;
if (FAILED(SafeArrayAccessData(psa, (void HUGEP**)&pbstr)))
return;
for (UINT i = 0; i < psa->rgsabound[0].cElements; i++)
{
CString s = pbstr[i];
AfxMessageBox(s);
}
SafeArrayUnaccessData(psa);
}
I tried it with VS6 and it seemed to work fine. Hope that helps,
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
Wow - that's a lot of code, thanks for going to this effort for me.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
|
I create a table A in the Oracle.
CREATE TABLE A
(
ID NUMBER(7) primary key
);
Now ,I want to insert a reocrd in the table A;
_ConnectionPtr pConn=NULL;
pConn.CreateInstance(__uuidof(Connection));
_RecordsetPtr pRst=NULL;
pRst.CreateInstance(__uuidof(Recordset));
pConn->Provider="OraOLEDB.Oracle.1";
try
{
pConn->Open("","ISVISION","ISVISION",NULL);
pRst->CursorLocation=adUseClient;
pRst->Open("A",pConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdTable);
pRst->AddNew();
pRst->Fields->Item[0L]->Value=100L; //But it throw a excpetion here. I dont know the reason. If I alter the datatype of ID to CHAR(10),it is all right.
//这里就会出现异常,如果表A的ID的数据类型是CHAR(10)的,又不会出错?
pRst->Update();
pRst->Close();
pConn->Close();
}
catch (_com_error& e)
{
MessageBox(e.Description(),"error",MB_OK|MB_ICONWARNING);
return ;
}
if (pConn)
pConn.Release();
if (pRst)
pRst.Release();
|
|
|
|
|
I am trying to maintain someone elses code and do not fully understand it's nuances. However I am trying to recover a string stored in a CComboBox list.
I posted this at little earlier at 19:50 but some of the code was missing/corrupted
//---------------------------------
void Control::DoDataExchange(CDataExchange* pDX)
{
Sheet* pSheet = ((C_App*)AfxGetApp())->getSheet();
MyPropertyPage::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Control)
DDX_Control(pDX, IDC_OTHER_COMBO, m_otherCombo); // IDC_OTHER_COMBO is the CComboBox ID:
//}}AFX_DATA_MAP
}
//---------------------------------
//Sheet::PopulateOtherCombo()
{
int i;
int entry=0;
int index=0;
CString name;
for (i=0;i<maxcount;i++)
{
="" some="" code="" that="" constructs="" the="" string="" 'name'
=""
="" write="" 'name'="" to="" ccombobox
index="getControl()-">m_otherCombo.AddString(name);
if (index != CB_ERR)
{
getControl()->m_otherCombo.SetItemData(index, entry++);
}
}
}
//---------------------------------------------------------------------------
//So far I have built a list of strings stored sequentially by 'index' but how do I recover a string ??
// When I click on the CComboBox, the list opens and I can select any of the entries.
int index = getControl()->m_otherCombo.GetCurSel();
// Gets the index # of the currently selected CComboBox entry.
// Now I want to copy the text of the currently selected entry somewhere else.
// I have the index but there is no GetString(index) function
// HELP!
Thanks for any help you can offer
Charles
|
|
|
|
|
The code gets 'corrupted' because you didn't check the box that says 'Do not treat <'s as HTML tags'. Edit this post so we can see the code, then you may get a reply
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
new to the forum. I'll look for it next time. meanwhile Brad Bruce suggested GetLBText. thanks anyway
|
|
|
|
|