Archive: Terminating string


Terminating string
Hello,
I need to read a string with fix size and compare it:

   !define SameText     "SameText"
!define SameTextSize 9
System::Alloc ${SameTextSize}
System::Call "MyDll::ReadMem(.r14, ${SameTextSize}-1) .r13"
StrCmp $R3 0 CallFailed
; -->> Terminate $R4 with '\0', BUT HOW?
StrCmp $R4 "${SameText}" 0 WrongData


I have no idea how to terminate my $R4. In C I just would do something like $R4[8]='\0'; But, with NSIS? Does anybody know?

Claus

err, if $R4 is a normal nsis string you can just do StrCpy $R4 $R4 9 or whatever


Sorry, there's a little bug in my code. i forgot to pop $R4 after allocation:

   !define SameText     "SameText"
!define SameTextSize 9
System::Alloc ${SameTextSize}
Pop $R4
System::Call "MyDll::ReadMem(.r14, ${SameTextSize}-1) .r13"
StrCmp $R3 0 CallFailed
; -->> Terminate $R4 with '\0', BUT HOW?
StrCmp $R4 "${SameText}" 0 WrongData


So $R4 isn't a normal NSIS string.

Claus

Mmmh, no answers?

No, my question isn't answered so far. As you can see, I allocate 9 bytes in $R4 and read the first 8 bytes via MyDLL::ReadMem. To compare with "SameText" the string in $R4 must be null terminated. How can I do this?


you can use the struct handling in system.dll, something like: system::call '*$R4(&t8,&i1 0)' (untested code off the top of my head)

but that is rather pointless since you need to get it out of the memory pointer and into a nsis var before you can StrCmp it, so get the string with something like system::call '*$R4(&t8 .r0)' and StrCmp $myfoo $r0


Originally posted by Anders
you can use the struct handling in system.dll, something like: system::call '*$R4(&t8,&i1 0)' (untested code off the
top of my head)

Okay, I have replaced
System::Alloc ${SameTextSize}
Pop $R4

with
System::Call "*$R4(&t8,i 0)"

but that is rather pointless since you need to get it out of the memory pointer and into a nsis var before you can StrCmp it, so get the string with something like system::call '*$R4(&t8 .r0)' and StrCmp $myfoo $r0
I'm not sure if I understand right: I cannot StrCmp my R4? It works (and worked) like a charm if my data is null terminated and if I read the terminating null via MyDll. (There were some changes in other code so it may happen that there is no terminating null. So I like to do it without the null. But this hasn't to do anything with NSIS.)

Claus

i did not mean replace, it was just for adding the 0 terminator, but yeah, you can alloc the struct like that, but it would be: System::Call "*(&t8,i 0).R4"


Thank you, Anders, for your help. I'm doing now:

!define SameText "SomeText"   
!define SameTextSize 8
System::Alloc ${SameTextSize}+1
Pop $R4
System::Call "MyDll::ReadMem(.r14, ${SameTextSize}) .r13"
; fill SameTextSize bytes into $R4
StrCmp $R3 0 CallFailed
System::Call "*$R4(&t${SameTextSize},&i1 0)"
StrCmp $R4 "${SameText}" 0 WrongData
; expected data found

Claus

I don't understand why you want to zero terminate this string when you can just extract it from the struct like i posted in my 2nd example, just stick .r0 after &t${SameTextSize} and use $0 in the strcmp


Do you mean the following?


!define SameText "SomeText"
!define SameTextSize 8
System::Alloc ${SameTextSize}
Pop $R4
System::Call "MyDll::ReadMem(.r14, ${SameTextSize}) .r13"
; fill SameTextSize bytes into $R4
StrCmp $R3 0 CallFailed
System::Call "*$R4(&t${SameTextSize} .r0)"
StrCmp $0 "${SameText}" 0 WrongData
; expected data found

Yes, I can do it, if this works. But I wasn't really sure that $0 is null terminated this way.

Claus