|
Hello!
In my MFC programm on Visual C++ 2008 i have the following problem with opening a file for reading:
-------------------------------------------
1) First step i do is to unzip a existing file (sSourceFile) containing my data with CZipArchive
Here to simplify matters without exception handling:
CZipArchive ZipArchive;
ZipArchive.Open(sSourceFile, CZipArchive::zipOpenReadOnly);
for (int i = 0; i < ZipArchive.GetCount(); i++)
{
ZipArchive.SetPassword(key);
ZipArchive.ExtractFile(i, GetProgramPath()); // GetProgramPath() is a own method which returns the current path
}
ZipArchive.Close();
-------------------------------------------
2) After that i rename the unzipped file:
DeleteFile(sTargetFile);
CFile::Rename( sSourceFile, sTargetFile ); // both, sSourceFile and sTargetFile, with full qualified path
So far it works without problems. The sTargetFile exists (reviewed by debugging).
-------------------------------------------
3) Now i want to open the renamed file for reading
CFile file;
file.Open(sTargetFile, CFile::modeRead);
-------------------------------------------
After that on some machines (with Windows 7) i get an CFileException::sharingViolation exception.
But on the most machines it works without an exception.
What is the matter for this exception?
Does the CFile::Rename still block the file?
How i can wait until the file is definitively closed?
Please help me!
modified on Sunday, January 31, 2010 5:51 AM
|
|
|
|
|
|
good idea, but the problem is a time-critical affair. This means as soon as i have for example a built-in debug message after CFile:Rename and CFile:Open, the problem disappears. So i can not look which process has the file between CFile:Rename and CFile:Open.
Any other help there?
|
|
|
|
|
Quick question - I'm wondering how I can detect when the user presses the enter key on the edit control in my dialogBox(), ie what messages do i need to handle in the dialog procedure?
|
|
|
|
|
WM_COMMAND with LOWORD(wParam) == IDOK and (HWND)lParam == your edit control HWND.
cheers,
AR
|
|
|
|
|
thanks for that! Works beautifully 
|
|
|
|
|
You're welcome 
|
|
|
|
|
You may also want to investigate whether the ES_WANTRETURN setting is appropriate to your control.
MVP 2010 - are they mad?
|
|
|
|
|
my control is a single-line edit box, so ES_WANTRETURN won't be needed 
|
|
|
|
|
Hi all,
i m using this to set background color of menu.
To change the background color of MenuBar, simply add below's line of code, beginning of mainframe OnCreate
CBrush* NewBrush;
NewBrush = new CBrush;
NewBrush->CreateSolidBrush(RGB(139,137,137));
MENUINFO MenuInfo = {0};
MenuInfo.cbSize = sizeof(MenuInfo);
MenuInfo.hbrBack = *NewBrush; // Brush you want to draw
MenuInfo.fMask = MIM_BACKGROUND;
MenuInfo.dwStyle = MNS_AUTODISMISS;
CMenu* pMenu = this->GetMenu();
if(IsMenu(pMenu->m_hMenu))
{
SetMenuInfo(pMenu->m_hMenu, &MenuInfo);
}
after using this,color of menu bar is changed but the color of menu items not changed.
please tell me how can i change the menu items color.
thanks in advance.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|
|
Use owner-drawn menus. There are plenty of samples - just search the C++ articles for 'owner-drawn menu'.
L u n a t i c F r i n g e
|
|
|
|
|
Hi,
In my application i have funtion like
char* CTagController :: SendInitialPoints()
{
union UController CON;
memset(CON.Buffer,0,CONSIZE);
CON.S.AlarmPriority =AlarmPriority;
CON.S.AlarmEnable =AlarmEnable;
CON.S.TagIndex =TagIndex;
.
.
.
.
return CON.Buffer;
}
In another function,im using this
char *pConInitData = 0 ;
pConInitData = pTagController->SendInitialPoints();
My problrem is,When i set Common Language Runtime Support (/clr) option in CLR support,
the pConInitData shows some garbage value.
If i set No Common Language Runtime support,then pConInitData returns correct value.
Whats the problem in setting /clr option.
Actually i convert VC6 to VS2008.Actually i want to set that /clr option,because im using some WPF controls in my applcaiton.
Pls help me
Anu
|
|
|
|
|
It looks like you are returning a pointer to an area of memory that is part of a stack variable. Thus, when SendInitialPoints() returns CON is eligible for garbage collection and may disappear. You should allocate your buffer using new to ensure it does not disappear before you have finished with it.
MVP 2010 - are they mad?
|
|
|
|
|
You can either allocate memory for the union on the heap as Richard said or you can create it on the stack from the caller function and pass it into the SendIntialPoints function.
char* CTagController::SendInitialPoints(UController& CON)
{
memset(CON.Buffer,0,CONSIZE);
CON.S.AlarmPriority =AlarmPriority;
CON.S.AlarmEnable =AlarmEnable;
CON.S.TagIndex =TagIndex;
.
.
.
.
return CON.Buffer;
}
This can be called as follows -
char *pConInitData = 0 ;
union UController CON;
pConInitData = pTagController->SendInitialPoints(CON);
In this case you do not need the function to return anything.
So the calling can be as follows -
union UController CON;
pTagController->SendInitialPoints(CON);
|
|
|
|
|
Hi Guys,
I got a txt file which contains html codes in it.
<td class="ticker_name"><a href="http://finance.yahoo.com/q;_ylt=Al7he7j4xkWAM99.PPB7UVFO7sMF;_ylu=X3oDMTE5cnE2OWVuBHBvcwMzBHNlYwNtYXJrZXRTdW1tYXJ5SW5kaWNlcwRzbGsDbmFzZGFx?s=%5EIXIC" >Nasdaq</a></td><td><span class="streaming-datum" id="yfs_l10_^ixic">2,147.35</span></td><td class="ticker_down"><span class="streaming-datum" id="yfs_c10_^ixic">-31.65</span></td><td class="right_cell ticker_down"><span class="streaming-datum" id="yfs_pp0_^ixic">-1.45%</span>
how do i extract the value example 2,147.35 and -31.65 of NASDAQ
|
|
|
|
|
You could write your own parser or adapt from this article[^].
MVP 2010 - are they mad?
|
|
|
|
|
 My immediate approach would be to:
(a) Scan for and replace all comas with nothing. I.e "," --> ""
(b) Scan for the text ixic">
(c) if string not found, then exit loop - jump to (g)
(d) Advance the returned pointer by the length of the search string (6 bytes)
(e) Do a scanf, asking for a float
(f) Return to (b)
(g) ...
Perhaps a little something like this?
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
#include <string>
using namespace std;
string& str_replace(const string &search, const string &replace, string &subject)
{
string buffer;
int sealeng = search.length();
int strleng = subject.length();
if (sealeng==0)
return subject;
for(int i=0, j=0; i<strleng; j=0 )
{
while (i+j<strleng && j<sealeng && subject[i+j]==search[j])
j++;
if (j==sealeng)
{
buffer.append(replace);
i+=sealeng;
}
else
{
buffer.append( &subject[i++], 1);
}
}
subject = buffer;
return subject;
}
int main()
{
FILE *fp;
char *htmlStr, *tmp, *filename="infile.html";
char *pos1, *pos2, *pos3;
float retrievedNum;
long fileSize;
string findMe = "ixic\">";
fp = fopen(filename, "r+b");
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
htmlStr = new char[fileSize+1];
htmlStr[fileSize] = 0;
fread(htmlStr, sizeof(char), fileSize, fp);
string tmpS = htmlStr;
string find = ",";
string replace = "";
tmpS = str_replace(find, replace, tmpS);
printf("%s\n\n", tmpS.c_str() );
fclose(fp);
strcpy(htmlStr, tmpS.c_str() );
pos1 = htmlStr;
while (pos1 = strstr(pos1, findMe.c_str()))
{
pos1 += strlen(findMe.c_str());
sscanf(pos1, "%f", &retrievedNum);
printf("Retrieved: %f\n", retrievedNum);
}
delete htmlStr;
}
Yeah, the code's not winning any beauty pageants. 
|
|
|
|
|
benjamin yap wrote: how do i extract the value example 2,147.35 and -31.65 of NASDAQ
Have a look at regular expressions (RE). There are various libraries for C++, see Boost or CodeProject articles about it. You could scan line by line trough your HTML-input and with a regular expression test/extract the wanted information.
Hope this helps,
M
|
|
|
|
|
Hi,
I am writing an Installer for Software Upgrades. The final step is to Delete the existing Bin Directory, and renaming the Temp One to the Old Dir Name. This will ofcourse fail if the existing Bin Dir contains Files that are in Use.
The First part of the question is, How can I force Terminate running Apps.(and then finalise the installation, and restart what was running. No problem with doing that last step)
The Second part is, When the above fails, I would like to Shut down the computer, restart it, Block everything while I finalise the initial Installation, before the 'Start' list executes, and then resume Normal Startup.
The third part is, 'Can that be done remotely on a Networked Computer.'
BTW We Don't use '.COM' or 'ActiveX'. The preferred solution would entail the 'Kernel' API functions.
Regards,
Bram van Kampen
|
|
|
|
|
Hi all,
I have been using the following method to allow multiple instances of a class CInd1 to be generated using a base class pointer with various virtual functions in the base class. The problem I have is that I now would like to access variables in the class CInd1 generated by new. I can't figure out how to do this. The pointer m_pIndicator points to the desired class but it's a CIndicator class pointer (necessary to use virtual functions). Is there any way to re-cast the pointer or similar to gain access to CInd1 variables?
Thanks
class CInd1 : public CIndicator
{
Public:
virtual void CInd1::Draw(..) const;
CInd1(..);
CInd1 () {}
};
class CIndicator : public CObject
{
public:
virtual void Draw(CDC* pDC) const {}
public:
CIndicator() {};
};
CIndicator* m_pIndicator;
m_pIndicator = new CInd1(..);
m_pIndicator-> ??
|
|
|
|
|
What you are experiencing is a warning that you probably have a design problem.
If you are working on objects via pointers to a base class, generally all that the code that uses the base class pointer should care about is the base class interface, irregardless of whether the actual object is of base class or derived type.
The immediate questions that your post brings up are: Why are you using a pointer to a dynamically allocated object here? Is there a better design available?
If you are really stuck with this yucky situation the next questions are: How do I know the what the actual type of object this pointer is pointer to is? What do I do in which case of what derived type it is? Can't I come up with a better design that avoids the mess I may be creating?
The best thing to do would probably be to share the situation & your design goal, so that people can give you advice on other ways of dealing with it.
If all my ranting and raving doesn't dissuade you, then you probably want to look up and familiarize yourself with the C++ dynamic_cast<> operator.
|
|
|
|
|
You are absolutely right Avi and I'm glad you brought this up. This design is typical for simple, graphical elements that are not typically modified (easier to delete and create a new one). However, I extended it to more complex elements that should have the capability to be modified since there is a lot of dialog setup. The real value of the base class implementation is that it allows the use of virtual functions, especially the draw function without which the View class draw routine becomes very complex.
Currently, if the user wants to modify the element(with a dialog), my code deletes it and replaces it with a new element (transparent to the user). The problem arises when the user creates 2 of the same kinds of elements but with different settings. Then, when the dialog corresponding to the element is opened, the dialog settings do not always correspond to the instance that was selected since the dialog will just retain the last settings. So my intent was to set the dialog controls based on the dialog data stored with the element when the dialog init function is called. I can't use a virtual function from CIndicator since there are other elements (besides CInd1) that have different dialogs and thus different set up vars.
There must be an accepted way to design this function, since many engineering tools allow lots of copies of elements to be used yet they can still be modfied. I'll give it some thought.
|
|
|
|
|
I think the (sort of) obvious answer here is that the derived class should generate it's own dialog, i.e.
m_pIndicator->IndDialog();
The IndDialog() function would be a virtual function. This way the class is completely self contained.
Currently the dialog for each element is instantiated in an unrelated class.
|
|
|
|
|
Now that I've gotten back here to look at this again and as far as I understand your situation, it sounds like you've come up with a reasonable plan. You are (obviously) entirely right in not wanting to bring up a dialog displaying the wrong initial data.
In addition, it seems to solve another (potential) problem. I don't know how you are having the user indicate which object to modify, but without something like this, you might have also had a problem determining which dialog to create. Now, at least as I am imagining it, you can hit test to determine what object has responsibility and not additionally need the user to specify the type of thing he wants to deal with. Perhaps this isn't an issue in your current UI, but with design changes, who knows what will come up?
Good luck with this.
|
|
|
|
|
Why not instantiate m_pIndicator to be a CInd1() object? This gives you access to all of the variables and methods in the base (except private) and derived class.
MVP 2010 - are they mad?
|
|
|
|
|