News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Help Initializing Structure

Started by asmftw, March 30, 2008, 06:14:42 PM

Previous topic - Next topic

asmftw

Hey, I made a structure like this:


CONNECTION STRUCT
  dwSock   dd          ?
  WSAData  WSADATA     <>
  Sockaddr sockaddr_in <>
  szState  db          64 dup(?)
  szName   db          64 dup(?)
  dwPort   dd          ?
CONNECTION ENDS


I'm trying to predefine szName in one instance of the structure, but I don't know how to fill in the null values for WSAData and Sockaddr, I also don't know how to make szState uninitialized.


So far all I got working is this:

connectionInstance CONNECTION <NULL>

I want do do something like this:

connectionInstance CONNECTION <NULL,NULL,NULL,NULL,"Connection1",NULL>

jj2007

Try this one, it is straightforward.

include \masm32\include\masm32rt.inc

.data?
CONNECTION STRUCT
  dwSock dd ?
  WSAData WSADATA <>
  Sockaddr sockaddr_in <>
  szState db 64 dup(?)
  szName db 64 dup(?)
  dwPort dd ?
CONNECTION ENDS
connectionInstance CONNECTION <>

.data

AppName db "Ciao", 0
TheNameIs db "Connection1", 0

.code
start:
xor eax, eax
mov connectionInstance.dwSock, eax
mov connectionInstance.dwPort, eax
invoke lstrcpy, addr connectionInstance.szName, addr TheNameIs
invoke MessageBox, 0, addr connectionInstance.szName, addr AppName, MB_OK
invoke ExitProcess,0
end start


sinsi

A structure within a structure needs to be initialised with <> around it, so

.data
connectionInstance CONNECTION <0,<>,<>,,"Connection1",80>

That will initialise:
dwSock as 0
WSAData as the defaults for the WSADATA structure
Sockaddr as the defaults for the sockaddr_in structure
szState as the default (empty string)
szName as "Connection1"
dwPort as 80

You can also initialise the WSAData by filling in the first set of empty <>
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: sinsi on March 31, 2008, 09:09:50 AM
WSAData as the defaults for the WSADATA structure
Where do these defaults come from? Could not find any entry in \masm32\include...

sinsi

The usual default is ? (undefined). You can give a structure default values when you declare it -

numbers struct
one   dd 1
two   dd 2
dunno dd ?
numbers ends

.data
num1 numbers <>      ;gives you the defaults (dd 1,2,?)
num2 numbers <2,4,6> ;gives you the 'overrides' (dd 2,4,6)
num3 numbers <,1,1>  ;gives you a (dd 1,1,1)
num4 numbers <,,>    ;gives you the same as num1

If you use the defaults, and the defaults are defined (i.e. not ?), it needs to be in the .data section and not the .data? section.
Light travels faster than sound, that's why some people seem bright until you hear them.

asmftw

Quote from: sinsi on March 31, 2008, 09:09:50 AM
A structure within a structure needs to be initialised with <> around it, so

.data
connectionInstance CONNECTION <0,<>,<>,,"Connection1",80>

That will initialise:
dwSock as 0
WSAData as the defaults for the WSADATA structure
Sockaddr as the defaults for the sockaddr_in structure
szState as the default (empty string)
szName as "Connection1"
dwPort as 80

You can also initialise the WSAData by filling in the first set of empty <>

Thank you! This will save a lot of useless coding to fill in things that should be set initially. Lol jj2007 you have the wrong idea, that way you have a zero buffer and are just filling it with zeros and a string at the start. The way sinsi says, all the data is already there. Anyways topic solved, thanks again.

sinsi

Quote from: asmftw on March 31, 2008, 11:27:58 AM
Lol jj2007 you have the wrong idea, that way you have a zero buffer and are just filling it with zeros and a string at the start.
If you declare your structure as a local variable, you need to do what jj2007 has done, and potentially even more - a field called dwReserved might need to be initialised to 0 before an API call, but locals are on the stack and could have anything as their value, so don't be too quick to dismiss another way  :naughty:
Light travels faster than sound, that's why some people seem bright until you hear them.