Hello forum members
How do I define the STRUCT, be one of the variables constant. like:
s Struct
A_ dd 25 ; < ---- Const and read only
B_ dd 13 ; < ---- Const and read only
C_ dd ?
D_ dd ?
s ends
, Or a pointer to a constant, where I can use directly, Like:
.const
s0 Struct
A_ dd ?
B_ dd ?
s0 ends
t s0 <25,13>
.data?
s Struct
t0 s0 <t> ; <--------- const
C_ dd ?
D_ dd ?
s ends
Or a change (dynamic ) Attribute of "A_ and B_" variable in the structure "s" to become read-only
This works, but you cannot set selected elements to read only:
s Struct
A_ dd 25 ; < ---- Const and read only
B_ dd 13 ; < ---- Const and read only
C_ dd ?
D_ dd ?
s ends
.data
MyS s <>
Well, you can put it in the .code section to make it read-only, but then also C_ and D_ are read-only...
Hi jj2007
What I want to do is to make A_ and B_(only) a read-only, and leave the rest as is
I've understood what you want, but it's not possible. Why do you want it that way?
I want to create (object)
where :variables, constants, and functions.
In the form of STRUCT
For example:
Obj struc
A_ DWORD 10 ;< -- Const
B_ DWORD 255 ;< -- Const
C_ Byte ? ;< -- Variable
D_ Word ? ;< -- Variable
F01_ Lp1TDF ? ;< -- Function
F02_ Lp2TDF ? ;< -- Function
Obj ends
You can do that, no problem. Just don't write to A_ and B_ ...
A structure is just an area in memory. You cannot write-protect 8 bytes and leave the following n bytes unprotected. Check VirtualProtect for more info.
I am using the function "VirtualProtect" (dynamic), but has disables all access to A_ and B_
I want to also use the method works with all platforms windows
changing memory attributes is on a "per-page" basis
on all current versions of windows, a page is 4 Kb
in assembly language, it's a little bit unnecessary to write-protect variables
that's because the programmer has complete control over what is and isn't written to
Actual such attribute (const) does not require the memory to be read only - it is the compiler (in our case this would be the assembler) who check the "access direction" while compiling and throws errors when writing to read-only members. Unless not replacing all instructions with macros who does the check, I can't see a chance to implement such feature with MASM.
In my test, using the VC++ Toolkit 2003 compiler, my attempt to modify a const structure member in C code was flagged as an error, but I could modify the member in inline assembly, so clearly it was not stored as read-only.
#include <windows.h>
#include <conio.h>
#include <stdio.h>
struct
{
int x;
int const y;
} s;
int main( void )
{
s.x = 1;
//s.y = 2; // test.c(14) : error C2166: l-value specifies const object
__asm mov s.y, 3
printf("%d\n", s.y);
getch();
}
And same result for this version:
#include <windows.h>
#include <conio.h>
#include <stdio.h>
struct S
{
int x;
int const y;
};
int main( void )
{
struct S s = {1,2};
s.x = 1;
//s.y = 2; // test.c(15) : error C2166: l-value specifies const object
__asm mov s.y, 3
printf("%d\n", s.y);
getch();
}