|
I disagree with the other answer. I think that, yes, you should push and pop any registers that you are going to touch. The compiler will take care of the rest automatically.
As an exercise, you should turn assembly code listings on for a small project and examine some of the code emitted by the compiler. In can be very educational for things like this.
-edit- if you have doubts then comment off the push and pops and see what happens. The phrase "unexpected results" comes to mind. However, if you change ECX without restoring it the results will be not unexpected - the app will most likely crash since the this pointer is different.
|
|
|
|
|
Hi All,
I have a problem with LoadImage method in my small Visual C++ project in Visual Studio 2008. No matter what I pass as filename, The GetLastError always returns "The System Cannot find the File Specified".
I've tried with Loading from the resource:
m_pBitmap->LoadBitmap(IDB_BITMAP1); //THIS WORKS PERFECTLY
But I've commented out this line and instead using the following code in OnInitialUpdate() method. But It fails. I've placed the bmp file in C: also in the Debug directory from where I run the executable. (While running from Debug the filename is simply "barcode.bmp" and I've placed the bmp file in the Debug directory.)
But I always get the error: The System Cannot find the File Specified. Definitely, I'm missing something but I'm unable to find out....
----------------------------------------------------------------------------------
void CMymfcBitmapView::OnInitialUpdate()
{
.
.
.
//m_hBitmap is a HBITMAP Member Variable in the my View Class
m_hBitmap = (HBITMAP)LoadImage(NULL, (LPCWSTR)"c:\\barcode.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(m_hBitmap == NULL)
{
//DWORD err = ::GetLastError();
LPVOID lpMsgBuf;
::FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, (LPCWSTR)"Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
--------------------------------------------------
Thanking you all in Advance....
|
|
|
|
|
raich wrote: m_hBitmap = (HBITMAP)LoadImage(NULL, (LPCWSTR)"c:\\barcode.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
Is the LPCWSTR cast necessary?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
Otherwise my compiler complains...
------------------------------------------
error C2664: 'LoadImageW' : cannot convert parameter 2 from 'char *' to 'LPCWSTR'
And It shows LoadImageW!!
------------------------------------------
Casting with (LPCWSTR) at least made my code compilation error free....
|
|
|
|
|
raich wrote: Casting with (LPCWSTR) at least made my code compilation error free....
But that does not always mean it's the right thing to do. Have you tried prefacing the string literal with an L ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
This error C2664 predicts your runtime error; this is the reason.
You should change the path encoding from ascii to unicode.
|
|
|
|
|
I'd say you're passing an Ansi string to something expecting Unicode (ie: wide strings).
Try:
L"C:\\barcode.bmp" or
_T("C:\\barcode.bmp")
-- Ian
|
|
|
|
|
Thank you Ian...It worked
And David thanks to you also....
|
|
|
|
|
raich wrote: m_hBitmap = (HBITMAP)LoadImage(NULL, (LPCWSTR)"c:\\barcode.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
Should be (as implicitely suggested by David)
m_hBitmap = (HBITMAP)LoadImage(NULL, _T("c:\\barcode.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
[added]
Well, not the fastest post of my life, I guess...
[/added]
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi all,
As you probably know, there is a problem with CString types in CList, CMap or CArray with regards to serialization. I have attempted to remedy this by overriding void AFXAPI SerializeElements. Before I post my code though, I'd like to ask if any other MFC container class can serialize CStrings correctly, for example CTypedPtrList? I'm not real particular on the container class for my current app and figuring out the peculiarities of serialize is exhausting.
The problem with the code below is that even after overriding SerializeElements, I get a mem copy error when doing a serialize load operation. A breakpoint in SerializeElements is never hit so for some reason it's not being used. I also have a serialize() function for the base class elements but it's not used either, presumably because all base elements are CSTRINGs? There are 2 classes, one for the base elements and another with the CList and functions. BTW, the functions that save and load the list are called from inside the Doc class serialize function.
Thanks in advance -
///////.h file
class CChartInfo : public CObject
{
DECLARE_SERIAL(CChartInfo)
public:
CString m_ChartName;
CString m_Portfolio;
CString m_DataPath;
CString m_DataFile;
CChartInfo(const CChartInfo &s)
{m_ChartName = s.m_ChartName;
m_Portfolio = s.m_Portfolio;
m_DataPath = s.m_DataPath;
m_DataFile = s.m_DataFile;}
CChartInfo& operator=(const CChartInfo &s)
{m_ChartName = s.m_ChartName;
m_Portfolio = s.m_Portfolio;
m_DataPath = s.m_DataPath;
m_DataFile = s.m_DataFile;
return *this;}
public:
void Serialize(CArchive& ar);
public:
CChartInfo();
virtual ~CChartInfo();
};
class CChartManager : public CObject
{
DECLARE_SERIAL(CChartManager)
public:
CList<cchartinfo, cchartinfo&=""> m_ChartList;
void AddChart(CChartInfo ChartInfo);
bool GetChart(CString ChartName, CChartInfo &ChartInfo);
bool LoadChartList();
bool SaveChartList();
CChartManager();
virtual ~CChartManager();
};
void AFXAPI SerializeElements(CArchive& ar,CChartInfo* pChartInfo, int nCount);
////////////////////////////////////////.cpp file
IMPLEMENT_SERIAL(CChartInfo, CObject, VERSION_NUMBER)
IMPLEMENT_SERIAL(CChartManager, CObject, VERSION_NUMBER)
CChartInfo::CChartInfo()
{
}
CChartInfo::~CChartInfo()
{
}
void CChartInfo::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if (ar.IsStoring()){
ar << m_ChartName
<< m_Portfolio
<< m_DataPath
<< m_DataFile;
}
else{
ar >> m_ChartName
>> m_Portfolio
>> m_DataPath
>> m_DataFile;
}
}
CChartManager::CChartManager()
{
}
CChartManager::~CChartManager()
{
}
// CChartManager member functions
bool CChartManager::SaveChartList()
{
//assumes m_ChartList has been loaded with correct data
char acPath[256];
if ( GetModuleFileName( NULL, acPath, 256 ) != 0) {
// guaranteed file name of at least one character after path
* ( strrchr( acPath, '\\' ) + 1 ) = '\0';
//AfxMessageBox( acPath ); // Use it
}
strcat(acPath, "ChartManager.dat");
CFile ChartManagerFile;
CFileException Err;
if (ChartManagerFile.Open(acPath, CFile::modeCreate | CFile::modeWrite, &Err))
{
CArchive archive(&ChartManagerFile, CArchive::store);
m_ChartList.Serialize(archive);
archive.Close();
ChartManagerFile.Close();
return 1;
}
else {
Err.ReportError();//for debug only
return 0;
}
}
bool CChartManager::LoadChartList()
{
//read chartlist file and load data into list
//if file not found returns false
m_ChartList.RemoveAll();
char acPath[256];
if ( GetModuleFileName( NULL, acPath, 256 ) != 0) {
// guaranteed file name of at least one character after path
* ( strrchr( acPath, '\\' ) + 1 ) = '\0';
//AfxMessageBox( acPath ); // Use it
}
strcat(acPath, "ChartManager.dat");
CFile ChartManagerFile;
CFileException Err;
if (ChartManagerFile.Open(acPath, CFile::modeRead, &Err))
{
CArchive archive(&ChartManagerFile, CArchive::load);
m_ChartList.Serialize(archive);
archive.Close();
ChartManagerFile.Close();
return 1;
}
else {
Err.ReportError();//for debug only
return 0;
}
}
void AFXAPI SerializeElements(CArchive& ar, CChartInfo* pChartInfo, int nCount)
{
for(int i=0;nCount;i++,pChartInfo++)
{
pChartInfo->Serialize(ar);
}
}
modified on Wednesday, September 23, 2009 3:09 PM
|
|
|
|
|
Ok,
I put a breakpoint inside the MS version of SerializeElements and it hit. So as suspected my version is not being used.
I guess I don't really understand how to override SerializeElements so the code uses my version. What are the rules?
The CList is still required to use .Serialize() of course.
Thanks
modified on Wednesday, September 23, 2009 4:38 PM
|
|
|
|
|
Ok,
Need to review Templates. You have to make the template explicit by adding the class name before (.
|
|
|
|
|
Is it possible to make the shell notify you when the user opens a folder and eventually prevent it? I am trying to build a folder guard app. Notification objects does not seem to cover this functionality. The ICopyHook interface also seems like not the solution. Any suggestions will be highly appreciated.
Martin
|
|
|
|
|
Have you read through Michael Dunn's Shell-related articles?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
See this article[^] by Raymond Chen? Although not exactly the same situation, the advice is still applicable.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Yes, just use a SW hook (classic code, Win32)
|
|
|
|
|
Hi Gurus,
I have a simple application, which pops a message box if it is invoked in a pre-logon mode (i.e. when the system displays the login screen).
How do I get to know if the application is invoked before the user is logged on??
I tried using
GetProcessWindowStation() - but this doesn't help.
NOTE: The application will be invoked from a service.
Thanks in advance.
Sunil
|
|
|
|
|
How about GetUserName() or NetWkstaGetInfo(102) or <code>NetWkstaUserEnum() ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
Hello,
I have written automation to start excel charts but the createdispatch doesnt start excel
_Application app;
if(!app.CreateDispatch(CreateDispatch("Excel.Application")
{
AfxMessageBox(
"Couldn't start Excel and get an application 0bject");
return;
}
wht can be the problem
Prithaa
|
|
|
|
|
prithaa wrote: if(!app.CreateDispatch(CreateDispatch("Excel.Application")
This is both syntactically wrong and just plain weird looking. How about:
COleException e;
if (! app.CreateDispatch("Excel.Application", &e))
... prithaa wrote: wht can be the problem
Did you remember to call AfxOleInit() ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
Hello
Oh sorry CreateDispatch was written twice
I wrote
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't start Excel and get an application 0bject");
return;
}
The above is not working
Prithaa
|
|
|
|
|
prithaa wrote: The above is not working
Why not? Are you looking at the exception object (specificaly it's getter method) like I suggested?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
Hello
Yes still excel doesnt start
COleException e;
if(!app.CreateDispatch("Excel.Application",&e))
{
AfxMessageBox("Couldn't start Excel and get an application 0bject");
return;
}
i wrote the above
|
|
|
|
|
Did you somehow think that simply creating the object would make it all better? What's the "value" of e ?
What type of object is app ?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
it is like this
_Application app;
e is not initialised
|
|
|
|