assemblymasmnull-terminatedgetmodulefilename

Null-terminate a string in MASM / GetModuleFileName


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?


Solution

  • 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.