PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Anonymous on February 20, 2006, 03:51:55 PM

Title: Forms in DLL
Post by: Anonymous on February 20, 2006, 03:51:55 PM
I'd like to put a basic set of forms in a dll. How do I exchange global variables that I use in these forms now. Can I add extra parameters to the forms-functions for this purpose?
Title: Forms in DLL
Post by: TechSupport on February 20, 2006, 11:04:41 PM
Create a function in your DLL that will accept the incoming variables. Just make sure to EXPORT it so that your exe can see it.

In your DLL:

Function SetVariables( AllVariables As GLOBAL_TYPE ) Export As Long


End Function


In your EXE:

Declare Sub SetVariables Lib "MYDLL.DLL" Alias "SETVARIABLES" _
           ( AllVariables As GLOBAL_TYPE )
Title: Forms in DLLS
Post by: Anonymous on February 27, 2006, 06:52:24 PM
Thanks a lot.

I've managed to put the specific standard forms in the DLL I'm writing.
Title: Question about AllVariables As GLOBAL_TYPE
Post by: Mark Strickland on February 28, 2006, 09:28:51 AM
Does this imply every variable in your main EXE becomes GLOBAL to the DLL?

If so how does it handle name conflicts among various functions?

If not does it just export the GLOBALS?

In the DLL carefully naming can handle conflicts if it makes everything GLOBAL.

Thanks.
Title: Forms in DLL
Post by: TechSupport on February 28, 2006, 10:32:12 AM
The variables in the EXE and DLL are totally separate. One can not access the other. That is the reason that he wants to send the variables to the DLL (in order to access them). Therefore, you can have the same names without fear of any interference. The hassle becomes having to send the variables to the DLL, manipulate them as you need, and then send them back to the calling EXE.
Title: Returning the variables from the forms in DLL
Post by: Anonymous on March 23, 2006, 08:17:45 AM
I'm using this technique now and it works fine. I'd like to return the values inthe struct (TYPE) back to the application. I can do that with deperate finctions that read the struct in the DLL but I'd like to do that in one strocke (function).

Is that possible? Do I have to use pointers???

It would be great if I got some example code.

Thanx in advance.
Title: Forms in DLL
Post by: TechSupport on March 23, 2006, 09:13:28 AM
TheEXE.bas

#Compile Exe

#Include "TheTYPE.inc"


Declare Sub SetVariables Lib "THEDLL.DLL" Alias "SETVARIABLES" (  AllVariables As GLOBAL_TYPE  )


Global MyTYPE As GLOBAL_TYPE


Function PBMain() As Long

  MyTYPE.nVar3 = "Paul Squires"

  ' Send the entire global MyTYPE to the DLL where
  ' it can be manipulated in the DLL.
  SetVariables MyTYPE
 
  ' Display what the value is now after it was changed in the DLL
  MsgBox MyTYPE.nVar3,,"EXE"
 
End Function


TheDLL.bas

#Compile Dll

#Include "TheTYPE.inc"

Global pType As GLOBAL_TYPE Ptr


Sub SetVariables( AllVariables As GLOBAL_TYPE ) Export

   
   MsgBox AllVariables.nVar3,,"DLL-IN"
   
   ' You can directly modify the incoming AllVariables Type
   ' elements and the changes will be sent automatically back
   ' up to the calling EXE (because it was sent to the DLL
   ' by reference, ByRef ).
   AllVariables.nVar3 = "Paul Squires - changed"

   ' Alternatively.....
   ' If you want other functions in your DLL to modify the
   ' incoming AllVariables TYPE, you can assign it to a
   ' pointer and directly manipulate it that way. In this
   ' case I assign it to a global pointer called pType.
   
   pType = VarPtr(AllVariables)
   
   @pType.nVar1 = 12345
   @pType.nVar2 = 19.99
   @pType.nVar3 = "3rd Type element changed via a pointer"
   
End Sub


TheTYPE.inc


Type GLOBAL_TYPE
  nVar1 As Long
  nVar2 As Single
  nVar3 As Asciiz * 100
End Type
Title: Forms in DLL
Post by: Anonymous on March 23, 2006, 02:03:22 PM
If you want to make a custom control dll I'm prefer to use handle technic
because it can protect your internal code

In Exe
- Init your custom control to register it class and to allocate internal dll struct
    ex. InitMyControl(initvar)
- Place your custom control
- Use custom Handle and Id to get your control handle
- hwnd=Gethandle(Handle,Id)

so to set or get variable you have to use separate function/sub
- SetnVar1(hwnd,nVar1)
- nVar1=GetnVar1(hwnd)

In dll
sub InitMycontrol(byval initvar as string) 'ex for password
  if initvar=passvar then registermyclass
end sub

Type Mystruct
 nVar1 as Long
 nVar2 as Single
 nVar3 as string*4
End Type

you can alloc your struct when WM_CREATE in your callback

CASE WM_CREATE
     myhnd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,   sizeof(mystruct))

then store your custom control handle into the extra window memory of a window

       SetWindowLong hWnd, 0, myhnd

then create your interface

function Gethandle(byval hwnd as long,byval id as long)as long
  hwnd=getdlgitem(hwnd,id)
  Gethandle=getwindowlong(hwnd,0)
end function

sub SetnVar1(hwnd,nvar1)
  myhndl=getwindowlong(hwnd,0)
  @myhndl.nvar1=nvar1
end sub

function GetnVar1(hwnd)as long
  myhndl=getwindowlong(hwnd,0)
  GetnVar1=@myhndl.nvar1
end sub

This technic also allow you to use multiple control in exe
Hope it can help you