flat assembler
Message board for the users of flat assembler.

Index > Windows > problems in Displaying text in a richedit

Author
Thread Post new topic Reply to topic
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 26 Mar 2012, 16:19
i need to find a name(string) in a file and display all the infomation(contain in the file), relevent to the name, in a richedit control and if multiple instances of the name
found,informations of each instance must be displayed.
i tried with following(files are attached with this)But windows given a error!!
please help!!!!!


Description:
Download
Filename: tst.zip
Filesize: 4.11 KB
Downloaded: 420 Time(s)

Post 26 Mar 2012, 16:19
View user's profile Send private message Send e-mail Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 26 Mar 2012, 16:33
Why are you Free'ing the memory that you've just allocated and read the file to, in this section of the code (Line 171):

Code:
   mov     [fileSize],eax
      invoke  GlobalAlloc,0,fileSize
      mov     [dwBuffer],eax

  invoke  ReadFile,[hFile],[dwBuffer],fileSize,dwWritten,0
    invoke  GlobalFree,dwBuffer  ; <- .. This doesnt belong here.
    invoke  CloseHandle,[hFile]
    


Not only should you not free what youve just allocated (since you plan to use it in the next section of code, right?). But even when you do want to free it, you should be passing it [dwBuffer], not dwbuffer.

You also do this GlobalFree after the ReadFile on line 220, this time with dwBuffer2..

The CloseHandle is fine, because youve read the file into memory, but the Free should not be there, the Free should be at the end of your program when you've finished using that memory.

That doesnt seem to be your only problem, as commenting those out doesnt fix the program - but its at least one problem..

EDIT: So I tried debugging this.. Theres a bunch of issues.. First off, you keep passing some variables as name rather than [name], for example, when doing GlobalAlloc with the fileSize, that should be [fileSize], otherwise you're allocaing the size of the pointer - which works fine if you have enough memory, but its not what you want, and will probably crash at some point (although not on my machine..) Same thing for ReadFile, should be using [fileSize].

You then have some weird stuff going on in the ..search function:
1) Dont do call ..search, do jmp ..search at the end of the code (call means its going to push the return address onto the stack every time.. eventually you'll run out of stack, itll also break the next return you do..)
2) You're opening, allocating memory, and reading the file for every search.. theres no need to do this.. since when you open the file, you read the whole thing into memory, so just keep that memory in place, and do another strstr on it like you're doing.. but dont re-read/re-allocate the file.. theres no point (and will cause issues because you're overwriting global variables).

Saying that - I dont think I can fix this.. In my opinion it requires a re-write, I would maybe take a look at some previous windows RichEdit code (FASMW's code uses richedit if i remember correctly) and base it on that - I dont have enough experience with Windows API/RichEdit to fix whats happening there. Hopefully someone else might.
Post 26 Mar 2012, 16:33
View user's profile Send private message Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 27 Mar 2012, 12:06
Thank you very much gunblade.
I learnd lot from your response,and changed the code accordingly.
BUT to accomplish my goal what can I do?
If I remove the methode, Reading the file and allocating desired part of the file to the variable(using CreateFile and ReadFile) in ..search part ,there should be another methode to do that.(please someone tell me if there is)
and why windows giving error msg.
Please someone help Confused
Post 27 Mar 2012, 12:06
View user's profile Send private message Send e-mail Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 27 Mar 2012, 16:03
Ok, I changed the code like below(attached with this).But still windows gives a error.Can someone explain whay??? Shocked


Description:
Download
Filename: tst.zip
Filesize: 4.08 KB
Downloaded: 423 Time(s)

Post 27 Mar 2012, 16:03
View user's profile Send private message Send e-mail Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1398
Location: Piraeus, Greece
Picnic 27 Mar 2012, 18:53
msvcrt function calls (strlen, strncpy, e.t.c) needs cinvoke.

Focus on your .NEXT subroutine, do some tests.
e.g try to add each record separately.
Post 27 Mar 2012, 18:53
View user's profile Send private message Visit poster's website Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 28 Mar 2012, 11:01
Thanx Picnic,
But please can you explain what you mean by
'e.g try to add each record separately. '

Can anyone explain why windows giving error msg when there is a loop in that position(.NEXT subroutine) Confused
Post 28 Mar 2012, 11:01
View user's profile Send private message Send e-mail Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1398
Location: Piraeus, Greece
Picnic 28 Mar 2012, 12:41
mns wrote:
Thanx Picnic,
But please can you explain what you mean by
'e.g try to add each record separately. '


You're welcome mns.

Well, i saw that you are looping and using strstr function to search for strings and distinguish entries in hidden edx file.
Remove temporary the line 'jmp .. search', notice that you can now add 2 entries by pressing next button.
Also notice that if you modify the third record's Name field as 'amila', you can add 3 consecutive entries by pressing next, before crash.
These are few quick observations, maybe are useful, maybe not.
Re-check your code step by step, watch values.
Post 28 Mar 2012, 12:41
View user's profile Send private message Visit poster's website Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 30 Mar 2012, 11:06
If you dont already - I would recommend using OllyDbg (or similar.. but i find this works best for 32-bit debugging in windows) to "step through" your program, and as Picnic says, watch the variables. Thats how I started debugging your code, however there was too many errors in order to continue nicely..

I may give it another shot with your updated code. I think when you're faced with a problem where theres soo many errors, you're better off taking out some of the code, try it with less code, see how that works, then put the other stuff back in gradually and see what causes it to break.

Will let you know if i get anywhere with it, but as I say.. its been a while since I hit up the winapi properly..
Post 30 Mar 2012, 11:06
View user's profile Send private message Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 30 Mar 2012, 12:42
thanx Picnic & gunblade,I'm in the process of what you have suggested but still no luck.And I never used a debugger before Embarassed although having copies of several debuggers.
When started with less code and adding other instructions gradually,I think I found where the problem is.at the jmp instruction .which arise when loop goes more than 2 times.and I wondered problem arise due to the inadequate buffer size(data3-which i used to copy the string)i used malloc and alloc fuction to allocate a new memmory when using loop every time .But the result is the same error. Confused
Post 30 Mar 2012, 12:42
View user's profile Send private message Send e-mail Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 30 Mar 2012, 14:24
Right, I think I have it working. It depends on the kind of behaviour you want, but it no longer crashes..


The code should look like:

Code:
 .NEXT :

       mov      [data3],0

   ..search:
        cinvoke  strstr,[dwBuffer],name2
        cmp     eax,0
        je      ..ending

        mov     [data1],eax


        cinvoke  strstr,[data1],Sname
        cmp      eax, 0
        je       ..ending
        mov     [data2],eax
        sub     eax,[data1]
        add     eax,146
        mov     [nn2],eax
        sub     [data1],146

        ;invoke  ReadFile,[data1],[dwBuffer2],[nn2],dwWritten,0
        cinvoke  strncpy,data3,[data1],[nn2]

        invoke  GetWindowTextLength,[hwndRichEdit]
        invoke  SendMessage,[hwndRichEdit], EM_SETSEL,eax,eax
        invoke  SendMessage,[hwndRichEdit],EM_REPLACESEL , FALSE ,data3
        mov     eax,[data2]
        mov     [dwBuffer],eax
        jmp     ..search

    ..ending:    


Basically: Change the invokes to cinvoke (not critical, but you waste stack by not cleaning it up yourself if you dont use cinvoke). and (importantly) add that cmp eax, 0/je ..ending after the second strstr. What was happening was that you were fnding all the results, but when you got to the end, you ended up getting a NULL back from that second strstr (since it cant find the next ==== delimiter), and then everything after that (strncpy/etc) would fail.. because your [nn2] "count" would be some random garbage number.

So yeah.. the current behaviour is that it results ALL search results in one page. So.. you run the program, you hit next and it brings up that white window, then you hit next, and it shows you all the results in one page (scroll down and you'll see that it includes both the 2 and 3 year old amila). You can then keep hitting next, but it will keep showing the same results..

If you were wanting instead to have one patient record per page.. (so, first time you hit next, it shows the first record, between the == and ==, the second time, it shows the second record, and then stops after that..) then you'll need to stop after the first succesfull strstr for the name that you're searching for, do the sendmessage to window, then when you hit next, you do the same again, if that is the behaviour you want, then let me know, it should be an easy change..
Post 30 Mar 2012, 14:24
View user's profile Send private message Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 30 Mar 2012, 17:21
Thank you very much gunblade solving this headache for me.
Actually this is a part of a programme(for learning perposes of win32 assembly) that I am making which help to save the details of a patient in a file(each for a day)and can recover the details of a patient when ever needed.For the time being your first solution is more than enough.Thanx again. Very Happy Very Happy
Post 30 Mar 2012, 17:21
View user's profile Send private message Send e-mail Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 31 Mar 2012, 00:52
No problem. As i say, if you want to solve issues like this in the future, a debugger like OllyDbg is vital.. You first run the program through the debugger, and see where it crashes, what instruction it was on, and what it was (a write to memory, a read from memory, a bad instruction?), then from there you can set breakpoints and trace the execution as it goes..

The other way, is to add "debug" lines in your code, where you print out the current status at some points.. easiest if you make it a small macro or similar that pops up a messagebox with current status and any registers you're wanting to watch.. although this means modifying the code, which could mess with any existing errors (or cause more), hence why the debugger is preferred.

Good luck with it, let us know if any more issues come up.. Smile
Post 31 Mar 2012, 00:52
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1659
Location: Toronto, Canada
AsmGuru62 31 Mar 2012, 11:40
In addition, the 'debugging' printout code can be included into conditional compilation clause(s),
so you can include it or exclude with just changing a value in just one line:
Code:
DBG_PRINT = 1   ; <-- put this at the top of the main source

...

if DBG_PRINT = 1
      stdcall PrintReg32, ecx
end if

...

if DBG_PRINT = 1
        stdcall PrintMem8, esi, 64
  stdcall PrintMem32, edi, 4
end if
    

Now, to switch off the printouts all you need to do is go to the top of the source and set that DBG_PRINT value to zero and re-assemble.
Post 31 Mar 2012, 11:40
View user's profile Send private message Send e-mail Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 01 Apr 2012, 15:54
Thax gunblade and AsmGuru62,for your kind suggestions.I've never thought debugging is so important in programming.And now I know it is a art must learn. Very Happy
Post 01 Apr 2012, 15:54
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.