DDE - C - sendmessage

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Patrick88
Messages : 1564
Inscription : mer. 21/janv./2004 18:24

DDE - C - sendmessage

Message par Patrick88 »

salut

je souhaite adapter du code C en purebasic
ce code permet de créer un lien DDE entre un prog externe et autocad

Code : Tout sélectionner

HWND hwnd_Acad;
WORD wm_acad;

char *ac = "\3\3ligne 0.0,0.0 3.4,4.5 6.6,7.7 \n";

wm_acad = registerwindowmessage("WM_ACAD");
hwnd_Acad = FindWindow("AUTOCAD.gfx",NULL);

If (hwnd_Acad != NULL)
  SendMessage(hwnd_Acad, wm_acad, 0, (LONG)(LPBYTE)ac) ;
pas de problème particulier pour "adapter" ce code
sauf sur la ligne
SendMessage(hwnd_Acad, wm_acad, 0, (LONG)(LPBYTE)ac)
ou je sais pas comment convertir (LONG)(LPBYTE)ac

Code : Tout sélectionner

Global chaine.s 
Global Acad_title.s

chaine = "ligne 0.0,0.0 3.4,4.5 6.6,7.7"+Chr(0)

Procedure ListWindows (window, parameter) 
    MemoryID = ReAllocateMemory (0, 255)
    GetClassName_ (window, MemoryID, 255) 
    class$ = PeekS (MemoryID) 
    
    MemoryID = ReAllocateMemory (0, 255)
    GetWindowText_ (window, MemoryID, 255) 
    title$ = PeekS (MemoryID) 

    FreeMemory (0) 
    
    ;Debug Chr (34) + title$ + Chr (34) + " / Class: " + class$ 
    If FindString(UCase(title$),"AUTOCAD",1)=1
      Acad_title = title$
    EndIf
    
    ProcedureReturn #True 
EndProcedure 

EnumWindows_ (@ListWindows (), 0)

CallDebugger
  
wm_acad.w = registerwindowmessage_("WM_ACAD")
hwnd_Acad.l = FindWindow_(0,Acad_title)
If hwnd_Acad<>0
  result.l = SendMessage_(hwnd_Acad,wm_acad,0,@chaine)  
EndIf

Debug "fini"
End
comtois
Messages : 5186
Inscription : mer. 21/janv./2004 17:48
Contact :

Message par comtois »

sur le site de Freak il y a une page qui précise comment convertir les principaux formats du C en purebasic , j'ignore si tu y trouveras ton bonheur , mais tu peux toujours essayer d'y faire un petit tour.
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Message par djes »

C'est pas tout bêtement un cast pour dire qu'il s'agit d'un pointeur long sur une chaine? Bref, tu peux mettre ta chaîne directement! Tu as essayé?

Par contre apparemment y'a pas que ça comme problème. Ta chaîne et ta fenêtre ne sont pas les mêmes que dans le code en C?!
Patrick88
Messages : 1564
Inscription : mer. 21/janv./2004 18:24

Message par Patrick88 »

-> comtois,

merci pour l'info, j'ai été sur la page web de freak et j'ai pus traduire

result.l = SendMessage_(hwnd_Acad,wm_acad,0,@chaine)

en

result.l = SendMessage_(hwnd_Acad,wm_acad,@chaine,len(chaine))

bon, ça marche pas pour autant, mais...

-> djes

hum, oui en effet, mais je n'ai pas trouvé la fenêtre "autocad.gfx" dans la liste des fenêtres des tâches.... surement pour ça que mon code ne marche pas. je n'envoie pas au bon client...

patrick
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Message par djes »

J'ai trouvé ça sur le net. Ca peut peut-être marcher...

Code : Tout sélectionner

Set WSHShell = WScript.CreateObject("WScript.Shell") 
cmdstr = "line" + Chr(13) + "0,0" + Chr(13) + "10,10" + Chr(13) + Chr(13) 
WSHShell.AppActivate "AutoCAD" 
WSHShell.SendKeys cmdstr
Patrick88
Messages : 1564
Inscription : mer. 21/janv./2004 18:24

Message par Patrick88 »

tu peux m'indiquer le lien ?

bon, à deuxième vue , il s'agit de code Visual Basic
or la commande SendKeys est propre à VB....

patrick
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Message par djes »

Sur le forum anglais, tu as toutes les explications pour faire la même chose, plus une petite fonction sendkeys que je n'ai pas testée mais qui a l'air top!

http://www.purebasicforums.com/english/ ... t=sendkeys

Edit: tout! sauf le lien
Dernière modification par djes le mer. 30/mars/2005 19:39, modifié 2 fois.
nico
Messages : 3702
Inscription : ven. 13/févr./2004 0:57

Message par nico »

Patrick88 a écrit :tu peux m'indiquer le lien ?

bon, à deuxième vue , il s'agit de code Visual Basic
or la commande SendKeys est propre à VB....

patrick
Non, c'est le langage de script window:WScript tout comme Vbscript
donc tu peux recopier le fichier au format texte avec l'extension wsh.
Avatar de l’utilisateur
djes
Messages : 4252
Inscription : ven. 11/févr./2005 17:34
Localisation : Arras, France

Message par djes »

Exact Nico! ;)
Patrick88
Messages : 1564
Inscription : mer. 21/janv./2004 18:24

Message par Patrick88 »

en mélangeant du code de droite et de gauche, cela finit par fonctionner ...

merci

patrick

Code : Tout sélectionner

; SendKeys procedure by PB -- do whatever you want with it.  :) 
; Syntax: r=SendKeys(handle,window$,key$) ; r = 0 for failure. 
; Specify either a handle or window$ title to type to, but not both! 
; You cannot type curly braces { } as part of the keystrokes, sorry! 

Procedure SendKeys(handle,window$,keys$) 
  If window$<>"" : handle=FindWindow_(0,window$) : EndIf ; Use window$ instead of handle. 
  If IsWindow_(handle)=0 ; Does the target window actually exist? 
    ProcedureReturn 0 ; Nope, so report 0 for failure to type. 
  Else 
    ; This block gives the target window the focus before typing. 
    thread1=GetWindowThreadProcessID_(GetForegroundWindow_(),0) 
    thread2=GetWindowThreadProcessID_(handle,0) 
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#True) : EndIf 
    SetForegroundWindow_(handle) ; Target window now has the focus for typing. 
    Sleep_(125) ; 1/8 second pause before typing to prevent fast CPU problems. 
    ; Now the actual typing starts. 
    keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT key before typing. 
    keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL key before typing. 
    keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT key before typing. 
    keybd_event_(#VK_LWIN,0,#KEYEVENTF_KEYUP,0) ; Release WINDOWS key before typing. 
    For r=1 To Len(keys$) 
      vk$=Mid(keys$,r,1) 
      If vk$="{" ; Special key found. 
        s=FindString(keys$,"}",r+1)-(r+1) ; Get length of special key. 
        s$=Mid(keys$,r+1,s) ; Get special key name. 
        Select s$ ; Get virtual key code of special key. 
          Case "ALTDOWN" : keybd_event_(#VK_MENU,0,0,0) ; Hold ALT down. 
          Case "ALTUP" : keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT. 
          Case "BACKSPACE" : vk=#VK_BACK 
          Case "CONTROLDOWN" : keybd_event_(#VK_CONTROL,0,0,0) ; Hold CONTROL down. 
          Case "CONTROLUP" : keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL. 
          Case "DELAY" : vk=0 : Sleep_(1000) ; Delay typing for one second. 
          Case "DELETE" : vk=#VK_DELETE 
          Case "DOWN" : vk=#VK_DOWN 
          Case "END" : vk=#VK_END 
          Case "ENTER" : vk=#VK_RETURN 
          Case "ESCAPE" : vk=#VK_ESCAPE 
          Case "F1" : vk=#VK_F1 
          Case "F2" : vk=#VK_F2 
          Case "F3" : vk=#VK_F3 
          Case "F4" : vk=#VK_F4 
          Case "F5" : vk=#VK_F5 
          Case "F6" : vk=#VK_F6 
          Case "F7" : vk=#VK_F7 
          Case "F8" : vk=#VK_F8 
          Case "F9" : vk=#VK_F9 
          Case "F10" : vk=#VK_F10 
          Case "F11" : vk=#VK_F11 
          Case "F12" : vk=#VK_F12 
          Case "HOME" : vk=#VK_HOME 
          Case "INSERT" : vk=#VK_INSERT 
          Case "LEFT" : vk=#VK_LEFT 
          Case "PAGEDOWN" : vk=#VK_NEXT 
          Case "PAGEUP" : vk=#VK_PRIOR 
          Case "PRINTSCREEN" : vk=#VK_SNAPSHOT 
          Case "RETURN" : vk=#VK_RETURN 
          Case "RIGHT" : vk=#VK_RIGHT 
          Case "SPACE" : vk=#VK_SPACE 
          Case "SHIFTDOWN" : shifted=1 : keybd_event_(#VK_SHIFT,0,0,0) ; Hold SHIFT down. 
          Case "SHIFTUP" : shifted=0 : keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT. 
          Case "TAB" : vk=#VK_TAB 
          Case "UP" : vk=#VK_UP 
          Case "WINDOWS" : vk=#VK_LWIN 
        EndSelect 
        If Left(s$,3)<>"ALT" And Left(s$,7)<>"CONTROL" And Left(s$,5)<>"SHIFT" 
          keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0) ; Press the special key. 
        EndIf 
        r+s+1 ; Continue getting the keystrokes that follow the special key. 
      Else 
        vk=VkKeyScanEx_(Asc(vk$),GetKeyboardLayout_(0)) ; Normal key found. 
        If vk>303 And shifted=0 : keybd_event_(#VK_SHIFT,0,0,0) : EndIf ; Due to shifted character. 
        keybd_event_(vk,0,0,0) : keybd_event_(vk,0,#KEYEVENTF_KEYUP,0) ; Press the normal key. 
        If vk>303 And shifted=0 : keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) : EndIf ; Due to shifted character. 
      EndIf 
    Next 
    If thread1<>thread2 : AttachThreadInput_(thread1,thread2,#False) : EndIf ; Finished typing to target window! 
    keybd_event_(#VK_MENU,0,#KEYEVENTF_KEYUP,0) ; Release ALT key in case user forgot. 
    keybd_event_(#VK_CONTROL,0,#KEYEVENTF_KEYUP,0) ; Release CONTROL key in case user forgot. 
    keybd_event_(#VK_SHIFT,0,#KEYEVENTF_KEYUP,0) ; Release SHIFT key in case user forgot. 
    keybd_event_(#VK_LWIN,0,#KEYEVENTF_KEYUP,0) ; Release WINDOWS key in case user forgot. 
    ProcedureReturn 1 ; Report successful typing!  :) 
  EndIf 
EndProcedure 

Global Acad_title.s

Procedure ListWindows (window, parameter) 
    MemoryID = ReAllocateMemory (0, 255)
    GetClassName_ (window, MemoryID, 255) 
    class$ = PeekS (MemoryID) 
    
    MemoryID = ReAllocateMemory (0, 255)
    GetWindowText_ (window, MemoryID, 255) 
    title$ = PeekS (MemoryID) 
   
    FreeMemory (0) 
    
    If FindString(UCase(title$),"AUTOCAD",1)=1
    ;If FindString(UCase(title$),"TEXTE AUTOCAD",1)=12
    ;If FindString(UCase(title$),"ADSKSC",1)=1
      Acad_title = title$
    EndIf
    
    ProcedureReturn #True 
EndProcedure 

EnumWindows_ (@ListWindows (), 0)

Debug Acad_title
hwnd_Acad.l = FindWindow_(0,Acad_title)
cmd.s = "cecolor 1 _LINE 0,0 100,100  "
SendKeys(hwnd_Acad,"",cmd)
Répondre