1

In my program I call the GetModuleFileName function from the Windows API. The function tells me the path of the running .EXE file.

On Windows XP machines the string (szSrc) is not null-terminated according to the MSDN.

invoke GetModuleFileName,NULL,szSrc,255

How can I null-terminate it?

Sep Roland
  • 33,889
  • 7
  • 43
  • 76

1 Answers1

1

You need to add a zero to your variable at the end.

.data
szSrc db "Your string", 0

If you need to do it at runtime, you need to get the length of your variable (szSrc), and then you could write something like this:

lea eax, szSrc
mov byte ptr [eax+szSrcLen], 0

Note : it is important to provide a valid length. If you don't know the correct string length then it will be impossible to make a null string.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
S.MAHDI
  • 1,022
  • 8
  • 15
  • Just for the record, `mov byte ptr [eax+szSrcLen], 0` assumes that `szSrcLen` is a placeholder that you replace with whatever register you actually have it in. Or that it's an EQU constant. (Or a bug in the asnwer). But *not* a memory location; it it's `sqSrcLen dd ?`, then you'd actually be indexing relative to where the length is stored, not using a length loaded from memory. x86 addressing modes don't have memory-indirect; if you want to use runtime-variable values as part of an addressing mode, they have to be in registers. e.g. `mov byte ptr [szSrc + ecx], 0` where `ecx` holds a length. – Peter Cordes May 09 '22 at 00:25