|
I also used atof(),but with atof() parameter in atof()function is char * but my string is Cstring.
|
|
|
|
|
If you're doing a Unicode build, you could use _ttof() and otherwise atof() will work. For example, this code will work for an MBCS build:
double df = 0;
CString str = "6.591E+02";
df = atof(str);
Hint: CString has the LPCTSTR operator defined, so the conversion will be done automatically.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
|
That's a misuse of CString::GetBuffer() .
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
camuoi288 wrote: atof() parameter in atof()function is char * but my string is Cstring That is not correct. From MSDN:
double atof(const char *string);
Since CString has LPCTSTR operator, you can easily call atof like this:
CString str = "1.234";
double d = atof(str);
|
|
|
|
|
1.
CString a = "6.591E+02";
float b;
sscanf(a,"%E",&b);
2.
CString a = "6.591E+02";
float b;
b = atof(a);
modified on Wednesday, April 20, 2011 6:03 AM
|
|
|
|
|
Since you're using C++ I suggest using streams.
#include <sstream>
{
CString strFloat("3.5");
istringstream is(strFloat);
float myFloat;
is >> myFloat;
}
This way you can catch errors easily.
Consider the following:
float af = atof("a");
float zerof = atof("0");
Both af and zerof will be 0, since on error atof() will return 0. Using streams you can check the stream status after conversion:
{
CString strFloat("3.5");
istringstream is(strFloat);
float myFloat;
is >> myFloat;
if (is.fail())
{
}
}
|
|
|
|
|
I am writing a MFC application which should run on different operating systems with different locales.
The application is in english and when running on english OS (like Windows 7) it works perfect.
However, when running on Chinese OS (Chinese Windows 7) I see that the english font is changed and it seems somewhat wider.
This wide font causes the text on my "Static Text" controls to be too big and the text is cut.
Is there a way to avoid this or must I go over all of my dialogs and set the size of the static text controls?
Regards,
Kobi.
|
|
|
|
|
|
I also have problem with localization. Chinese,Japanese, Korean and Thai Texts are displayed as Squares only in the Form Caption(Displayed perfect inside the Form). Also this problem occurs on running the application under Windows XP. There's no problem with Windows 7. What to do to localize the form caption for any Operating System?
|
|
|
|
|
I am downloading file via FTP ,but I am unable to find the file download
status .
I am not able to call OnStatusCallback function . How can I achieve my
task.
|
|
|
|
|
|
Hi all,
I have a very basic question to ask.
when i run this line of code
char array[27] = "abcdefghijklmnopqrstuvwxyz";
AfxMessageBox(array+15);
it gives this output pqrstuvwxyz.
I am not getting why it is doing so.....
I expected it to print only p.
I know i am wrong but please anybody who can explain me this.....
|
|
|
|
|
AfxMessageBox() takes a pointer to string (terminated by '\0' character) as argument. Thus it shows the characters till the '\0' character, from the pointer you provided.
Initializing a character array with a string appends a '\0' character to the end by default.
To show 'p' only:
char msg[2];
msg[0] = *(array+15);
msg[1] = '\0';
AfxMessageBox(msg);
It is basic thing and hope it is clear now.
|
|
|
|
|
AfxMessageBox takes a null terminated string as an in parameter. Since the termination happens only after the character z, it prints everything until then. If you want to print only the character p, the string should terminate after that character p.
Or you could extract the character of your interest ('p' in this case) into a temporary variable and use it.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
and if my array is 500 bytes long and i have some value in my array, and then i write this line of code
unsigned long test;
test = *(unsigned short int *)(array+ 36);
what should happen with this??
|
|
|
|
|
that will probably show you a value combined with the ASCII of 2 chars (or how much the size of short data type), starting from array[36].
If short is of size 2 bytes, then the hex value in test (assuming long variable is of 4 byte size) can be:
0x 00 00 Ascii-of-array[37] Ascii-of-array[36].
The reverse order (asccii of 36th char after 37th) is assuming the Little Endian byte ordering. Can differ in other byte ordering schemes.
|
|
|
|
|
What you are doing is known as pointer arithmetics. You take the pointer to the array and add 15 times the size of the array elements. In your case, sizeof(char) is 1, so array+15 will be the address of the 16:th character. The result is still a pointer, which you send to the message box, which in turn will read it till it finds the terminating \0 character.
|
|
|
|
|
this is my code.. it's not work properly.. i have downloaded this code from internet.. when i edit at file that i have downloaded.. i work properly.. but when i create a new form.. it's not work.. can anyone help me?
#include "stdafx.h"
#include "trans.h"
#include "transDlg.h"
#include "afxdb.h"
#include "odbcinst.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CtransDlg dialog
CtransDlg::CtransDlg(CWnd* pParent /*=NULL*/)
: CDialog(CtransDlg::IDD, pParent)
, drv(_T(""))
, pm(_T(""))
, cs1(_T(""))
, cs2(_T(""))
, cs3(_T(""))
, cs4(_T(""))
, cs5(_T(""))
, cs6(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CtransDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, drv);
DDX_Text(pDX, IDC_EDIT2, pm);
DDX_Text(pDX, IDC_EDIT3, cs1);
DDX_Text(pDX, IDC_EDIT4, cs2);
DDX_Text(pDX, IDC_EDIT5, cs3);
DDX_Text(pDX, IDC_EDIT6, cs4);
DDX_Text(pDX, IDC_EDIT7, cs5);
DDX_Text(pDX, IDC_EDIT8, cs6);
}
BEGIN_MESSAGE_MAP(CtransDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, &CtransDlg::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON1, &CtransDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CtransDlg::OnBnClickedButton2)
END_MESSAGE_MAP()
// CtransDlg message handlers
BOOL CtransDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CtransDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CtransDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CtransDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CtransDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
OnOK();
}
CString S, sp1, sp2, sp3, sp4, sp5, sp6;
void CtransDlg::OnBnClickedButton1()
{
CDatabase database;
CString SqlString;
CString sItem1, sItem2, sItem3, sItem4, sItem5, sItem6, sItem7, sItem8;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "H:\\project\\excel_odbc_demo\\book1.mdb";
// You must change above path if it's different
int iRec = 0;
UpdateData(true);
pm;
// Build ODBC connection string
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset( &database );
// Build the SQL statement
SqlString = "SELECT field_1, field_2, field_3, field_4, field_5, field_6, field_7, field_8 "
"FROM WorkSheet "
"ORDER BY field_1";
// Execute the query
recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
// Loop through each record
while( !recset.IsEOF() )
{
// Copy each column into a variable
recset.GetFieldValue("field_1",sItem1);
recset.GetFieldValue("field_2",sItem2);
recset.GetFieldValue("field_3",sItem3);
recset.GetFieldValue("field_4",sItem4);
recset.GetFieldValue("field_5",sItem5);
recset.GetFieldValue("field_6",sItem6);
recset.GetFieldValue("field_7",sItem7);
recset.GetFieldValue("field_8",sItem8);
if(sItem1 == pm)
{
S = sItem2;
sp1 = sItem3;
sp2 = sItem4;
sp3 = sItem5;
sp4 = sItem6;
sp5 = sItem7;
sp6 = sItem8;
}
// goto next record
recset.MoveNext();
}
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// If a database exception occured, show error msg
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
drv = S;
UpdateData(false);
}
void CtransDlg::OnBnClickedButton2()
{
UpdateData(true);
pm = S;
cs1 = sp1;
cs2 = sp2;
cs3 = sp3;
cs4 = sp4;
cs5 = sp5;
cs6 = sp6;
UpdateData(false);
}
the error is.. :-
Error 1 error C2440: 'initializing' : cannot convert from 'const char [32]' to 'ATL::CStringT<BaseType,StringTraits>' h:\trans\trans\transdlg.cpp 187 trans
Error 2 error C2440: 'initializing' : cannot convert from 'const char [37]' to 'ATL::CStringT<BaseType,StringTraits>' h:\trans\trans\transdlg.cpp 189 trans
Error 3 error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [31]' to 'const wchar_t *' h:\trans\trans\transdlg.cpp 197 trans
Error 4 error C2664: 'void CRecordset::GetFieldValue(LPCTSTR,CDBVariant &,short)' : cannot convert parameter 1 from 'const char [8]' to 'LPCTSTR' h:\trans\trans\transdlg.cpp 219 trans
Error 5 error C2664: 'void CRecordset::GetFieldValue(LPCTSTR,CDBVariant &,short)' : cannot convert parameter 1 from 'const char [8]' to 'LPCTSTR' h:\trans\trans\transdlg.cpp 220 trans
Error 6 error C2664: 'void CRecordset::GetFieldValue(LPCTSTR,CDBVariant &,short)' : cannot convert parameter 1 from 'const char [8]' to 'LPCTSTR' h:\trans\trans\transdlg.cpp 221 trans
Error 7 error C2664: 'void CRecordset::GetFieldValue(LPCTSTR,CDBVariant &,short)' : cannot convert parameter 1 from 'const char [8]' to 'LPCTSTR' h:\trans\trans\transdlg.cpp 222 trans
Error 8 error C2664: 'void CRecordset::GetFieldValue(LPCTSTR,CDBVariant &,short)' : cannot convert parameter 1 from 'const char [8]' to 'LPCTSTR' h:\trans\trans\transdlg.cpp 223 trans
Error 9 error C2664: 'void CRecordset::GetFieldValue(LPCTSTR,CDBVariant &,short)' : cannot convert parameter 1 from 'const char [8]' to 'LPCTSTR' h:\trans\trans\transdlg.cpp 224 trans
Error 10 error C2664: 'void CRecordset::GetFieldValue(LPCTSTR,CDBVariant &,short)' : cannot convert parameter 1 from 'const char [8]' to 'LPCTSTR' h:\trans\trans\transdlg.cpp 225 trans
Error 11 error C2664: 'void CRecordset::GetFieldValue(LPCTSTR,CDBVariant &,short)' : cannot convert parameter 1 from 'const char [8]' to 'LPCTSTR' h:\trans\trans\transdlg.cpp 226 trans
Error 12 error C2678: binary '+' : no operator found which takes a left-hand operand of type 'const char [17]' (or there is no acceptable conversion) h:\trans\trans\transdlg.cpp 250 trans
|
|
|
|
|
It looks like you're trying to do an Unicode build, but your code is written to compile only for ANSI builds. Use generic data types and functions (TCHAR instead of char , _tcscpy instead of strcpy , etc.,). And include string literals within the _T macro. This will help you do both Unicode and ANSI builds without having to change the code.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
For an item of hardware I have written a Shared library to make it easy to use the API for this hardware. The library sets up data structures and handles, buffers etc for simple program use.
For example to arrange data to be transmitted I call routines like:-
UINT16 TransmitData( UINT unit, UINT addr, TXBUFFER * tx_buffer );
However in VS2008 I used C++ in a MFC dialog application, which all worked well.
Having moved to VS2010, I now see that the intellisence does not work with this product for C++.
So my thoughts were to use C# for the Windows forms and link in the Shared Library.
My question is do I need to general a DLL rather than a Shared Library so that I can use C++, C# and perhaps VB. Is this correct and what are the procedures to follow.
The libray is built using C. I include the API.lib and API.h along with the DLL that came with the API s/w environment.
Many thanks,
Andy
|
|
|
|
|
|
Hi,
I'm trying to create a deskband (windows xp). The class which implement
IDeskBand is CBand. I've created some brush in
the constructor of this class. And destroyed in destructor.
right click taskbar->toolbars-> select MyBand, now my deskband is visible.
then right click taskbar->toolbars->MyBand, now my deskband is gone.
But the gdi items are still in memory ? It seems
the destructor is not called.
If repeat show, hide deskband, the gdi object count increases with every time.
I've created it as specified in the following url.
http://msdn.microsoft.com/en-us/library/cc144099(v=vs.85).aspx
CBand::CBand()
{
MessageBox(0, "Construct", 0,0) ;
m_lRef = 1 ;
}
CBand::~CBand()
{
MessageBox(0, "Destruct", 0,0) ;
}
DWORD __stdcall CBand::AddRef()
{
return InterlockedIncrement(&m_lRef) ;
}
DWORD __stdcall CBand::Release()
{
if( InterlockedDecrement(&m_lRef) == 0 )
{
delete this ;
}
return m_lRef ;
}
HRESULT __stdcall CBand::QueryInterface( REFIID riid , void** ppvObj )
{
*ppvObj = NULL;
if(IsEqualIID(riid, IID_IUnknown))
{
*ppvObj = this;
}
.
.
.
.
if(*ppvObj)
{
(*(LPUNKNOWN*)ppvObj)->AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
HRESULT __stdcall ShellFactory::CreateInstance(IUnknown* pUnknownOuter , const IID& iid , void** ppv )
{
if( pUnknownOuter != NULL )
{
return ResultFromScode( CLASS_E_NOAGGREGATION ) ;
}
if( IsEqualCLSID(m_clsid , CLSID_MyBand))
{
CBand * pObj = new CBand() ;
if( pObj == NULL )
{
return E_OUTOFMEMORY;
}
HRESULT hRes = pObj->QueryInterface( iid , ppv ) ;
if(FAILED(hRes) )
{
pObj->Release() ;
}
return hRes;
}
return S_FALSE ;
}
Thanks & Regards
|
|
|
|
|
I am trying to call a function in one class from another class and when I do,
I can not get or set data that is in any of the Edit Controls in my dialog
of the function that I am calling.
I have multiple columns of simuliar controls that I want to copy from one to the others.
So I call my copy function and try to copy from one column to the selected others. Slider Controls,
Check boxes, and Radio Buttons seem to work fine, but I can not get the Edit contols to work.
Here is an example of the code I have:
class Dialog1 : public CDialog
{
public:
void CallCopyFnc();
private:
Dialog2* myDlg2;
}
class Dialog2 : public CDialog
{
public:
void CopyDataFnc();
}
Dialog1::OnInitData()
{
myDlg2 = new Dialog2;
}
Dialog1::CalllCopyFnc()
{
myDlg2->CopyDataFnc();
}
Dialog2::CopyDataFnc()
{
CString str;
str.Format("0x%08X", number);
EditCtrl.SetDlgItemTextA(idEditCtrl, str);
}
Any help would be appreciated as to what I am doing wrong. Thanks in advance!
David
modified on Tuesday, April 19, 2011 12:33 PM
|
|
|
|
|
Member 7814600 wrote:
Because the edit control does not exist until Dialog2::OnInitDialog() .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|