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

Everything else that doesn't fall into one of the other PB categories.
jrw
User
User
Posts: 24
Joined: Fri Jul 18, 2003 6:25 pm

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

Post 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
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post 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

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
jrw
User
User
Posts: 24
Joined: Fri Jul 18, 2003 6:25 pm

Post 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)
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post 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.
Image Image
jrw
User
User
Posts: 24
Joined: Fri Jul 18, 2003 6:25 pm

Many thanks!

Post by jrw »

Working great, Thankyou!!

Did I say?

Thankyou :lol:
Post Reply