|
|
Yes, I know that. I was asking if anyone had already written replacements or knew of anything else.
|
|
|
|
|
#define IsBadReadPtr(x) ((x) == NULL)
#define IsBadWritePtr(x) ((x) == NULL) These functions are that bad. An immediate null pointer check is about as good as it's going to get.
DoEvents: Generating unexpected recursion since 1991
|
|
|
|
|
For a read pointer all we need to know is if VirtualQuery can read it. So you could do something like this:
BOOL IsBadReadPointerCheck(char *p, size_t size)
{
MEMORY_BASIC_INFORMATION mbi;
for (void *pStop = p + size; p < pStop;p =p+mbi.RegionSize)
{
if(!VirtualQuery(p,&mbi,sizeof(mbi)))
{
return FALSE;
}
}
return TRUE;
}
For a write pointer we would need to iterate through the entire range and check the bits stored in the Protect member of the MEMORY_BASIC_INFORMATION structure. Something like this:
BOOL IsBadWritePointer(char *p, size_t size)
{
BOOL bRet = FALSE;
void *pStop = p+size;
MEMORY_BASIC_INFORMATION mbi;
for(p;p < pStop;++p))
{
if(VirtualQueryEx(GetCurrentProcess(),(LPVOID)p,&mbi,sizeof(MEMORY_BASIC_INFORMATION)))
{
if(!(((mbi.Protect & PAGE_READWRITE) == PAGE_READWRITE) || ((mbi.Protect & PAGE_EXECUTE_READWRITE) == PAGE_EXECUTE_READWRITE)))
{
return TRUE;
}
}
}
return bRet;
}
Lets say that you have a pointer to a member function and you want to validate the member function pointer. You might do something like this:
MEMORY_BASIC_INFORMATION mbi;
if(VirtualQueryEx(GetCurrentProcess(),(LPVOID)pInstructions,&mbi,sizeof(MEMORY_BASIC_INFORMATION)))
{
if(mbi.Protect & PAGE_EXECUTE_READ && mbi.State & MEM_COMMIT && mbi.AllocationProtect & PAGE_EXECUTE_WRITECOPY && mbi.Type & MEM_IMAGE)
{
(*this.*MemberFunction)(val);
return TRUE;
}
}
*Disclaimer Edit*
I have not had time to test these functions as I am currently too busy. These functions were removed from some experimental code I was working on some time back. Use/Modify them at your own risk. Just by looking over them I can see they need to have additional error handling.
Best Wishes,
-David Delaune
modified on Friday, May 2, 2008 3:05 PM
|
|
|
|
|
Hello,
i tried to load an unicode file but without luck, but i don't know whats wrong. The first char of the file will not be displayed correctly and then the following data is correct, but at the end there are some more charachters (rectangles) which are not in the file.
Here is the code i use:
<br />
wchar_t *gg;<br />
<br />
FILE *wfile;<br />
wfile = _wfopen(L"ReadMe.txt",L"rb");<br />
fseek(wfile,0,SEEK_END);<br />
int num = ftell(wfile);<br />
fseek(wfile,0,SEEK_SET);<br />
<br />
gg = (wchar_t*)malloc(sizeof(char)*num);<br />
fread(gg,sizeof(wchar_t),num,wfile);<br />
fclose(wfile);<br />
<br />
MessageBox(NULL,gg,L"file",0);<br />
Thanks for help.
|
|
|
|
|
You're allocating num * sizeof(char) and then trying to read num * sizeof(wchar_t) . That going to be your first problem
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
gabbana wrote: wfile = _wfopen(L"ReadMe.txt",L"rb");
For text files, should this be:
wfile = _wfopen(L"ReadMe.txt", L"r");
gabbana wrote: gg = (wchar_t*)malloc(sizeof(char)*num);
gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1);
gg[num] = '\0';
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
hmm, the problem still exists:
one rectangle at the beginning at many after the file (>16)
<br />
wchar_t *gg;<br />
<br />
FILE *wfile;<br />
wfile = _wfopen(L"ReadMe.txt",L"r");<br />
fseek(wfile,0,SEEK_END);<br />
int num = ftell(wfile);<br />
fseek(wfile,0,SEEK_SET);<br />
<br />
gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1);<br />
fread(gg,sizeof(wchar_t),num,wfile);<br />
fclose(wfile);<br />
<br />
gg[num] = '\0';
MessageBox(NULL,gg,L"file",0);<br />
modified on Friday, May 2, 2008 10:16 AM
|
|
|
|
|
The problem has to do with the FILE structure containing a char type rather than a wchar_t type. Convert from char to wchar_t , like:
char *gg = (char *) malloc(sizeof(char) * num + 1);
...
USES_CONVERSION;
LPWSTR lp = A2W(gg);
MessageBox(NULL, lp, _T("file"), MB_OK);
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Can you give me an example what you exactly mean?
|
|
|
|
|
okay, but now i only get 3 confusing symbols.
|
|
|
|
|
What does your modified code look like? Do you have UNICODE and _UNICODE defined?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
hi,
You might want to try:
gg = (wchar_t*)malloc(sizeof(wchar_t)*(num+1));
instead of
gg = (wchar_t*)malloc(sizeof(wchar_t)*num+1);
also, are you sure your file is in UNICODE format where each character is 16 bit wide?
|
|
|
|
|
In the project options i defined that the linke should use unicode instead of ascii.
Now i also tried yours but without an effect.
The file was written with notepad and i used the save as option and selected unicode as charset.
Okay but now, i am a little bit confusing. i am not sure if i need this for my further learning cycle? Iam actually learning the winapi, but i think for productional use it would be better if i directly use the unicode file stream classes or the windows createfile function to read a file or not ?
So i think its not important or i am wrong?
|
|
|
|
|
It actually all depends on the project requirements, but I suppose its better to create projects with UNICODE defined and keep your textual data in unicode format. Prefered in my opinion should be UTF-8 which uses less space.
If you are loading a text file you should somehow know its encoding type. If you are using Notepad to save text in Unicode format then file contains some extra information bytes about actual encoding type - it is easy to see them in binary file view. Some info on actual notepad format encoding can be found here:
http://blogs.msdn.com/michkap/archive/2007/04/22/2239345.aspx[^]
There are some other errors in your code that might be the cause of the errory you describe. For example ftell() return length of the file in bytes..., here is a code I got working quite well:
wchar_t *gg;
FILE *wfile;
wfile = _wfopen(L"c:\\ReadMe.txt",L"r");
fseek(wfile,0,SEEK_END);
int num = ftell(wfile);
fseek(wfile,2,SEEK_SET);
int buffSize = sizeof(wchar_t)*(num/2+1);
gg = (wchar_t*)malloc(buffSize);
memset(gg, 0, buffSize);
fread(gg,sizeof(wchar_t),(num-1)/2,wfile);
fclose(wfile);
|
|
|
|
|
In my program, I need to look a directory and copy the filenames in that directory and display the same in my own listbox for a User to select. I want the finding and copying the files names to be done without a Users intervention - strictly behind the scenes activity. Anyone know how I can do this. (VS2008 C++ in MFC.)
Code would be nice. I used CString extensively.
Thanks in advance.
|
|
|
|
|
Do you need to DlgDirList or FindFirstFile/FindNextFile?
|
|
|
|
|
Larry Mills Sr wrote: display the same in my own listbox for a User to select
WIN32_FIND_DATA would be one of the options along with FindFirstFile etc.
Larry Mills Sr wrote: I want the finding and copying the files names to be done without a Users intervention - strictly behind the scenes activity
didn't get how would the user be intervening in this process of finding the files once you have the name of the directory and so it's path.
Larry Mills Sr wrote: Code would be nice. I used CString extensively.
I have just started working on your requirement. Would give you an optimized code soon that is specific to VS2008 C++ in MFC. You can transfer some amount [in cash please] to my account for the work that I do for you.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
modified on Friday, May 2, 2008 8:47 AM
|
|
|
|
|
_AnShUmAn_ wrote: I have just started working on your requirement. Would give you an optimized code soon that is specific to VS2008 C++ in MFC. You can transfer some amount [in cash please] to my account for the work that I do for you.
don't put you account number publicly , hacker migth hack it
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/codeProject$$>
|
|
|
|
|
Use this code to find all files in a directory:
CString szFile, szDir = _T("D:\\CodeProject\\Trial\\res\\*.*");
CFileFind CFind;
BOOL bFound = CFind.FindFile(szDir);
while(bFound)
{
bFound = CFind.FindNextFile();
szFile = CFind.GetFileName();
CString ext3=szFile.Right(3);
CString ext4=szFile.Right(4);
if(!szFile.IsEmpty() && (szFile.GetLength()>4))
{
}
}
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Thank you very much.
A C++ programming language novice, but striving to learn
|
|
|
|
|
also try using multithreading extensivly
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You/codeProject$$>
|
|
|
|
|
Hi experts,
I have one application that enumearates ports..
when i buld my application and execute it runs fine..
but when i copied it to other PC
or another User of my PC then it gives me a exception
" The application failed to initialize properly (0x8000003). becoz of Congfiguration is incorrect.."
can anyone help me..its urgent...
what should I do?
Thnaks in advance..
|
|
|
|
|
Try decreasing the desktop resolution. Applications fail to initialize properly at very high screen resolutions. Also, try executing the application with different desktop wallpapers.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
ok
thanks for reply..
I already check all these things...
and my application is console based ..
so i think there is no need to change display settings,.
is there any other solution>
or any settings in VC++ project settings have to do?
reply.
|
|
|
|