Page 1 of 1

Trying to convert some c++ code to purebasic - help!

Posted: Fri Aug 01, 2003 8:21 pm
by jrw

Code: Select all

inline void Console(char *cgCmd) 
{ 
HWND MohCons = FindWindow(0, "Mohaa Console"); 
EnumChildWindows(MohCons, EnumChildSendCommandProc,(LPARAM)cgCmd); 
} 


BOOL CALLBACK EnumChildSendCommandProc(HWND hwnd,LPARAM lParam) 
{ 
char className[1024]; 

GetClassName(hwnd,className,sizeof(className)); 

if (strcmp("Edit",className)) return true; 
if (ES_READONLY & GetWindowLong(hwnd,GWL_STYLE)) return true; 

SendMessage(hwnd,WM_SETTEXT,0,lParam); 

SendMessage(hwnd,WM_CHAR,13,0); 
return 1; 

}
I wrote a loop so it prints the message to the console every couple of seconds. However purebasic crashes :(

Code: Select all

#PB_Window_ThickFrame=$40000
#WS_MAXIMIZE = $1000000
win=OpenWindow(0, 40, 40, 170,90, #PB_Window_ThickFrame | #PB_Window_SystemMenu ,"Window")
mohcon.l=0
cmd$="say hello console"

Procedure EnumChildSendCommandProc(hwnd, lParam) 
  Dim lpClassName.s(1024)
  Dim L.l(1024)
  exit=0
  lpClassName.s = Space(1024)
  L = GetClassName_(hwnd, lpClassName.s, 1024)
  If (LCase(className.s) <> "edit") Or ((GetWindowLong_(hwnd, #GWL_STYLE) And #ES_READONLY) = #ES_READONLY) 
    EnumChildSendCommandProc = True
    exit=1
  EndIf
  If exit<1
    If (GetWindowLong_(hwnd, #GWL_STYLE) And #ES_READONLY) : exit=1 : EndIf
  EndIf
  SendMessage_(hwnd,#WM_SETTEXT,0,lParam)
  SendMessage_(hwnd,#WM_CHAR,13,0)

  EnumChildProc = 1
EndProcedure


Repeat
  Sleep_(1)
  EventID.l=WindowEvent() 
  wParam = EventwParam() 
  If mohcon=0
    mohcon = FindWindow_(0, "Mohaa Console")
  EndIf
  b=b+1
  If b>400 : b=0 : EndIf
  If b=10 
    EnumChildWindows_(mohcon, @EnumChildSendCommandProc, 0)
  EndIf
  Select EventID
    Case #PB_EventCloseWindow
      quit=1
  EndSelect       
;
Until quit=1

Posted: Sat Aug 02, 2003 3:47 pm
by LarsG
well, I can't help you much, but changing:

Code: Select all

win=OpenWindow(0, 40, 40, 170,90, #PB_Window_ThickFrame | #PB_Window_SystemMenu ,"Window") 
to

Code: Select all

win=OpenWindow(0, 40, 40, 170,90, #PB_Window_ThickFrame | #PB_Window_SystemMenu ,"Mohaa Console") 
stops it from crashing...

-Lars

Posted: Sat Aug 02, 2003 5:29 pm
by jrw
Interesting..except the whole point of the application is to send text to the console of a game and not itself :lol:

The c++ code works so I must of made a mistake somewhere in the conversion but I cannot see it :( I know not everyone will have MoH but this works for any quake3 engine game you may have 8)

Posted: Sat Aug 02, 2003 6:33 pm
by Paul
Hello jrw,

Without having your MoH file it's quite hard to test but I might translate your C file to something like this:

Code: Select all

Procedure.l EnumChildProc(hWnd,lParam) 
  Ret=1
  lpClassName.s=Space(1024)
  GetClassName_(hWnd,@lpClassName,Len(lpClassName))
  
  If lpClassName="Edit" 
    Ret=0
  EndIf 
   
  If #ES_READONLY & GetWindowLong_(hwnd, #GWL_STYLE)
    Ret=0
  EndIf
    
  SendMessage_(hwnd,#WM_SETTEXT,0,PeekS(lParam)) 
  SendMessage_(hwnd,#WM_CHAR,13,0) 
 
  ProcedureReturn Ret 
EndProcedure

And then your main code to something like this:

Code: Select all

#PB_Window_ThickFrame=$40000 
#WS_MAXIMIZE = $1000000 
win=OpenWindow(0, 40, 40, 170,90, #PB_Window_ThickFrame | #PB_Window_SystemMenu ,"Window") 
mohcon.l=0 
cmd$="say hello console" 
 
Repeat 
  Sleep_(1) 
  EventID.l=WindowEvent() 
  wParam = EventwParam() 
   
  If mohcon=0 
    mohcon=FindWindow_(0,"Untitled - Notepad") 
  EndIf 
   
  b=b+1 
  If b>400 : b=0 : EndIf 
  If b=10 
    If mohcon
      EnumChildWindows_(mohcon,@EnumChildProc(),cmd$)
    EndIf 
  EndIf 
   
  Select EventID 
    Case #PB_EventCloseWindow 
      quit=1 
  EndSelect        
  
Until quit=1 
End
*Notice I used Notepad to test the code :)
You will have to change it to the proper window title.

Many thanks!

Posted: Sun Aug 03, 2003 12:42 am
by jrw
Working great, Thankyou!!

Did I say?

Thankyou :lol: