|
I am not sure what you are trying to do. Do you want to stop users of your software from using delete, rename, and formatting?
Well I do not have an answer but I do have a recommendation. The best book on advanced Windows programming is "Programming Applications for Microsoft Windows" 4th Edition by Jeffrey Richter.
Enjoy and good luck!
INTP
|
|
|
|
|
The question is not what you enable users to use but what you disable.
By hooking API functions you redirect execution of this functions to yours by modifying the Import Address Table in system dll's linked to executables.
The solution to your problem is therefore hooking all functions (e.g.:CopyFile, ReadFile, WriteFile) which you want users to not use.
A good place to start:
http://www.codeproject.com/useritems/api_monitoring_unleashed.asp[^]
Peter Molnar
|
|
|
|
|
When using int's, CString's, etc in my MFC app's I have declared the ones required for the life of the object in my class header file and then used them where needed in helper functions. Now I am tring to use CList templates (read newbie) in the following way
#include <afxtempl.h>
class whatever
{
public
void AddWords();
.
CList<cstring, cstring&=""> wordList;
.
.
};
whatever::AddWords
{
CString temp;
//CList<cstring, cstring&=""> wordList;
.
.
wordList.AddTail(temp);
.
.
}
when I do this I get an assertion error stating
if (!AfxIsValidAddress(pOb, sizeof(CObject)))
{
TRACE0("ASSERT_VALID fails with illegal pointer.\n");
if (AfxAssertFailedLine(lpszFileName, nLine))
AfxDebugBreak();
BUT, when I create wordList in the AddWords helper function (ie uncomment here and comment there) my code works fine.
I have trawled through books and websites to no avail for an answer so now think this must be a really stupid question. Never the less, I need an answer and would appreciate any help.
Thanks in advance.
Dave
|
|
|
|
|
Dave again, just to let you know Im not a complete numpty and the formatting performed by the HTML,PHP whatever editor removed part of my message. should have read
CList:CString, CString&: wordList;
where the : is the appropriate greater than or less than symbol. same for my include which is of course afxtempl.h
thanks
|
|
|
|
|
I just posted and deleted my post because I feel its incredibly dumb. (What I want to do I know how to with CSdioFIle and atof, but I want to try with fread). I have a txt file that looks like this:
0
0.0500
0.0990
0.1490
0.1980
0.2430
0.2870
0.3310
0.3740
0.4150
0.4530
0.4920
0.5300
with the leading spaces (4 spaces). I copied this data from matlab and pasted into notepad. I want to load this data into an array of doubles. I did the following but evidently I dont know how to use fread inspite of re-reading the docs and googling. It just doesnt click
FILE* fpRed = fopen("red64.txt", "r");
if (!fpRed ) return;
double red64[64];
while (!feof(fpRed))
{
for (int i = 0; i < 64; i++)
{
fread(red64+i, sizeof(double), 10, fpRed);
double r = red64[i];
}
}
fclose(fpRed);
I put down 10 because its 4 white spaces + 6 spaces in the number itself.
If theres invisible stuff in the file I dont know......
Thanks for helping,
ns
|
|
|
|
|
Are you sure there are four spaces at the beginning, and not one tab?
|
|
|
|
|
It backs up one by one four spaces. If it were a tab what would I do for nItemstoRead?
Anyways for the first few elements I removed the spaces so there numbers start flush at the left, and yet when I step through I get garbage. This time I said nItems = 6 since 0.5000 etc.
Thanks for helping,
ns
|
|
|
|
|
First of, fread fills a buffer you specify with characters read from the file. Characters are different from numbers. For example, a char number[2] = "32" is NOT the same thing as int a = 32
Secondly, a file is just lots of characters placed sequentically, with occasional control markers placed between. There are NO NUMBERS there. So you just cannot read doubles from a file. That just does not work.
Now, let's redo this thing..
FILE* fInput = fopen("red64.txt", "rt");<DIV>
if ( !fInput )
return;<DIV>
char buffer[20] = { NULL };
char number[8] = { NULL };
double red64[64];<DIV>
int nDoublePos = 0;<DIV>
while ( !feof(fInput) )
{
fgets( &buffer[0], 19, fInput );<DIV>
int nPosition = 0;
int nLinePos = 0;<DIV>
while ( buffer[nLinePos] != '\n' )
{
if ( isdigit( buffer[nLinePos] ) || buffer[nLinePos] == '.' )
{
number[nPosition++] = buffer[nLinePos];
}<DIV>
nLinePos++;
}<DIV>
if ( number[0] != NULL )
red64[nDoublePos++] = atof( &number[0] );<DIV>
memset( &buffer[0], 0, sizeof(buffer) );
memset( &number[0], 0, sizeof(number) );
}
Here's a basic idea on how the conversion is implemented. First we read a line to the buffer string. Then we step through the line, copying characters that are either numbers or a '.' character. Then we convert from a string of numbers to a double value using atof .
Don't use the above code blindly, check it for errors, as the forum formatting might've tricked me again
An alternative method would be to read the whole crap into one big buffer, then obtain a pointer to the start of that buffer and always when detecting a number, using atof to convert the number to a double.
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Thank you so much for your detailed response! I see I had totally misunderstood fread! I'll check it out. I had thought CStdioFile and atof was too circuitous but I now see that fread is also doing chars.....
thanks,
ns
|
|
|
|
|
Good answer!
Minor detail:
char number[2] = "32"; // Outch: char number[2] = {'3','2','\0'}
Should be
char number[3] = "32"; // Ok
even in our sleep.
Sorry automatic reaction when I see a line of code (even example code) that makes my heart skip a beat.
INTP
|
|
|
|
|
In an anonymous Citrix session authenticate to a printer (net use <printer> at a command prompt). Then execute code that calls
HDC hDC = CreateDC(NULL, <device name of printer>, NULL, NULL)
If the Citrix box is running Win2000 this works. A valid device context is returned and you can print to it.
If the Citrix box is running Win2003 the returned HDC is NULL, but GetLastError() also returns zero.
Does anyone know if this is a defect in the OS or how to get around it?
fred.
|
|
|
|
|
Hello,
I'm having trouble porting data. Here's the situation. An app written in VC++ has a data structure which uses MS data types (DWORD etc.) and standard tpyes. It defined a variable to be of this structure and wrote the data to a file. Now, I write an app in UNIX to read the original data back and load it into a local variable defined to be the same structure as before. Of course, UNIX does not contain types such as HANDLE. I tried my best to re-define the structure with equivalent data types. However, I run into a problem. I used sizeof to get the size of the structure in both app's. It is larger on windows. I assume it's due to the byte boundry issue when windows goes to allocate memory. The offending data type I think is __int64, but it's defined under both platforms (a long long I think). Any work around?? Thanks.
|
|
|
|
|
Humm..
I've created very little software for *nix platforms, but I'd consider that this problem is, like you stated, related to the new 64-bit architecture-compatible values. Following is an extract from MSDN:
"The Microsoft specific types __int8, __int16, and __int32 are synonyms for the ANSI types that have the same size, and are useful for writing portable code that behaves identically across multiple platforms. The __int8 data type is synonymous with type char, __int16 is synonymous with type short, and __int32 is synonymous with type int. The __int64 type has no ANSI equivalent."
As you see, there is no equivalence for __int64 in ANSI standard, so I doubt that the *nix compiler you are using supports the completely similar __int64 as MSVC++ uses. There is no existing standard here yet.. The 'long long' is at best a vague
My suggestion is that you go back to the original code and see if there are any values that you can redefine to 32-bit types. Other option would be to fetch a compiler that supports ILP64 data model standard. In there, 'int' and 'long' are 64-bit values, and 'int32' is a 32-bit value.
Alternatively, see if you compiler supports the "System Interfaces and Headers, Issue 5 (XSH)" definition. In here, a header 'inttypes.h' contains a type 'int64_t' for 64-bit integers.
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
There is also the problem of endianness. If you are dealing with binary data you might also actually have to reverse the byte order. This depends on the micro-processor not necessarily the operating system - but it is a problem you must look out for.
J.
----------------------------
|
|
|
|
|
If suitable, try and split the structure up into units that are common. For example, rather than an int64, have two 32bit integers. Depending on what you use it for, you'll then need to convert this into the appropriate value at the other end. This could be handled by a conditional statement:
#ifdef WIN32
convert for windows
#else
convert for unix
#endif
Not the nicest solution, but sometimes you have no choice but to get dirty!
|
|
|
|
|
I created a structure whose members are __int64 and DWORD. I used sizeof to find that __int64 is 8 while DWORD is 4. However, I get 16 when I do sizeof(my_structur_t). Shouldn't I get 12 (4 + 8)?? Thanks in advance.
|
|
|
|
|
Probably due to alignment. I guess the first member is DWORD, and _int64 is aligned on 8-byte boundary. Try it with _int64 first.
|
|
|
|
|
This might be a word alignment issue. You can include a #pragma statement to force it to 1 byte alignment, and there is something in the compiler options that lets you do this as well.
J.
----------------------------
|
|
|
|
|
The whole may be greater than the sum of its parts.
The compiler is trying to optimize the size and alignment of the structure as a whole, as well as the members within. In your case the compiler is taking the larget member (__int64), making the structure size a multiple if it (8) and making sure the members are memory aligned for thier respective type.
To get around this 'pack' your structure :
e.g.
#pragma pack(push, 1)
typedef struct {
__int64 pa;
DWORD pb;
} SA;
#pragma pack(pop)
However, remember there is a reason that the compiler is aligning the structure and its members (not going to get into it here). You will generally only pack structures (or classes) that mirror data on disk or in a stream (where the reader and writer may use different alignment/packing rules), or if you are creating massive instances of the structure and you want to save the memory (have yet to see a case where this reason is justified though).
...cmk
Save the whales - collect the whole set
|
|
|
|
|
I was wrong with my suggestion to change the sequence of the two definitions; the size is 16 in both cases.
I guess the reason is to ensure that the structure starts on the appropriate boundary even in an array of structures.
|
|
|
|
|
I have OnCtlColor for coloring controls. It works with all but the buttons. Additionally I have OnDrawItem; even if it does not do anything, the coloring made in OnCtlControl appears.
Can one explain it to me, why the coloring suddenly appears - only by virtue of declaring OnDrawItem?
But the more important issue is: text does not appear on the button - neither the originally defined text (via the button declaration), nor the text passed in OnDrawItem via DrawText.
DrawText returns the font size, i.e. apparently it runs properly - only, that the text does not appear.
|
|
|
|
|
To anyone interested:
the rectangle in the DrawText function has to be taken from DRAWITEMSTRUCT, because the position is relative to the button, i.e. the top left position is (0,0). First I passed the button position in the dialog window.
|
|
|
|
|
I ran into the same thing (kinda) a couple days ago.
If we think of there being 3 coordinate systems : screen, window, client then we see that most drawing functions are in client with the remainder in screen.
But both ExtTextOut() and DrawText() seem to use window coordinates (and of course the MS docs don't mention this).
A general solution is to get the client coord then adjust for any border. I whipped up a small function to get the border :
bool GetBorderRect( HWND H, RECT &R )
{
if( !H || !::IsWindow(H) ) return(false);
CkRECT rw(1000,1000,1000,1000);
::AdjustWindowRectEx(&rw,
::GetWindowLongPtrW(H, GWL_STYLE),
FALSE,
::GetWindowLongPtrW(H, GWL_EXSTYLE)
);
R.left = 1000 - rw.left;
R.top = 1000 - rw.top;
R.right = rw.right - 1000;
R.bottom = rw.bottom - 1000;
return(true);
}
I can then do something like (for custom static painting) :
CkRECT rc; ::GetClientRect(wndHnd, rc);
CkRECT rb; ::GetBorderRect(wndHnd, rb);
rc.MoveBy(rb.left, rb.top);
::ExtTextOutW(DC, rc.left,rc.top, ETO_OPAQUE, &rc, NULL, 0, NULL);
...
::DrawTextW(DC, s, -1, &rc, TxtStyle);
...cmk
Save the whales - collect the whole set
|
|
|
|
|
Thanks. Somewhere I calculated the recangle the same way you suggest; however in OnDrawItem it is not necessary, because the rectangle is part of the parameter.
The reason I'm writing this is another issue. You mentioned the different coordinate systems (it's a real joy, isn't it).
If you are working on products, which have to run on all 32-bit Windows, you may find following information useful.
I just wrote a program, which stretches respectively shrinks a dialog window (every control, fonts, etc.) on special user key actions, in order to adjust it to monitor resolution and magnification.
Anyway, I needed the size of the desktop to maximize the window size.
deskwnd = GetDesktopWindow();
(*deskwnd).GetWindowPlacement(&desktop);
Now, this works well on XP and Win98 (I have not tested it on Win2000), but on Win95 it returns a negative number for the width of the desktop (but the height is ok .
I tested it with different resolutions: the returned value is always the correct value - 5428.
I thought you might use this info.
|
|
|
|
|
Vancouver wrote:
however in OnDrawItem it is not necessary, because the rectangle is part of the parameter.
I don't use MFC anymore, i've got my own Win32 class framework. It is in my static window control, that does custom painting, where i needed this.
Vancouver wrote:
I just wrote a program, which stretches respectively shrinks a dialog window (every control, fonts, etc.) on special user key actions, in order to adjust it to monitor resolution and magnification.
Ouch, and i thought i was a sadist. Cool, but it must have been a pain to write.
Vancouver wrote:
Anyway, I needed the size of the desktop to maximize the window size.
Have you looked at :
GetSystemMetrics() with SM_CXSCREEN, SM_CYSCREEN, or GetDeviceCaps() with HORZRES, VERTRES (one calls the other) ?
... also when reading the doc's for GetSystemMetrics() check out SM_CXFULLSCREEN.
...cmk
Save the whales - collect the whole set
|
|
|
|
|