Export functions in an EXE (just like a dll) - uses coffIT

Share your advanced PureBasic knowledge/code with the community.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by ts-soft »

Make a doubleclick on "PB\v4.41\PureBasic.exe", close it and use coffit!

Greetings
Thomas
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Then coffIT is probably locating the 4.2 or 4.3 version of PB (the registry entries changed somewhere along the line - version 4 I think?)

Run the following program Kwai, it will tell you which version of PB coffIT is using.

Code: Select all

Procedure.s GetPBFolder(index=0)
  Protected hKey1.l, Type.l, Res.l, Folder$, lpbData.l, cbData.l, PBRegKey.s
  cbData = 1024  ;The PB entry is really quite long!
  lpbData = AllocateMemory(cbData)
  Folder$=""
  hKey1=0
  Type=0
  Res=-1
  If lpbData
    Select OSVersion()
      Case #PB_OS_Windows_95,#PB_OS_Windows_98,#PB_OS_Windows_ME
        PBRegKey="Software\Classes\PureBasic.exe\shell\open\command"
        Res=RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, PBRegKey, 0, #KEY_ALL_ACCESS, @hKey1)
      Case #PB_OS_Windows_NT3_51,#PB_OS_Windows_NT_4,#PB_OS_Windows_2000,#PB_OS_Windows_Server_2003
        PBRegKey="Applications\PureBasic.exe\shell\open\command"
        Res=RegOpenKeyEx_(#HKEY_CLASSES_ROOT, PBRegKey, 0, #KEY_ALL_ACCESS, @hKey1)
      Case #PB_OS_Windows_XP, #PB_OS_Windows_Vista,#PB_OS_Windows_Server_2008,#PB_OS_Windows_Future
        PBRegKey="Software\Classes\PureBasic.exe\shell\open\command"
        Res=RegOpenKeyEx_(#HKEY_CURRENT_USER, PBRegKey, 0, #KEY_ALL_ACCESS , @hKey1)
    EndSelect
    If Res = #ERROR_SUCCESS And hKey1
      If RegQueryValueEx_(hKey1, "", 0, @Type, lpbData, @cbData)=#ERROR_SUCCESS
        Folder$ = PeekS(lpbData)
        Select index
          Case 0
            Folder$ = GetPathPart(StringField(Folder$,2,Chr(34)))
          Case 1
            Folder$ = StringField(Folder$,6,Chr(34))
          Case 2
            Folder$ = StringField(Folder$,8,Chr(34))
          Case 3
            Folder$ = StringField(Folder$,10,Chr(34))
        EndSelect
      EndIf
      RegCloseKey_(hKey1)
    EndIf
    FreeMemory(lpbData)
  EndIf
  ProcedureReturn Folder$
EndProcedure

Debug GetPBFolder()
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by Kwai chang caine »

Hello MASTER TsSoFT 8)
The PB 4.41 run in the same time, and it's the one i use now :wink:

Thanks SROD...your IDIOT is always IDIOT
He push the F5 key of his keyboard to have your answer...and he don't see you have already answered but in the second page :oops:
And he wait ...wait...and cry alone in his desktop :(
And another little light in his brain...i see the litle "2" ..and i'm here with you two...on the second page :D

So i have tried the code
You have again right

Code: Select all

E:\Donnees\User\MES DOCUMENTS\Travail\[[PureBasic]]\v3.94\
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Okay Kwai, I will add a facility for manually setting the PB installation folder to over-ride that located by coffIT itself.
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by Kwai chang caine »

So your servant again surprising you...
He read with his eyes the bad REG KEY and found it :?
I have modify it, with big kick ass....

And you don't believe me ....that's works now...no error :shock: 8)

I continue to try your tips :wink:
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Kwaï chang caïne wrote:So your servant again surprising you...
He read with his eyes the bad REG KEY and found it :?
I have modify it, with big kick ass....

And you don't believe me ....that's works now...no error :shock: 8)

I continue to try your tips :wink:
That's one way around the problem for sure. :)
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by Kwai chang caine »

I have finish to try your splendid example.
That's works .. 8)

You have take the time to good explain, slowly, and preciously...
And IDIOTMAN have not understand, why it's interesting to convert an EXE for using it like a DLL ??? :roll:

Never mind...don't try to explain to me...because it's already difficult for me to understand in french.
So in english.. it's more difficult..and furthermore it's certainly an use for the MASTER :roll:
At my advice..it's again too difficult for my level
Perhaps i understand better a day, when i'm behind a problem and you say to me : "Try my example :twisted: "

Again thanks for your kindness 8)
ImageThe happiness is a road...
Not a destination
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

Well it just means that your EXE can have two uses; first as a standalone program and second as a shared library.

I admit that loading an EXE as if it were a dll is probably of little real use because ordinarily you would be better off siphoning the shared functions into a separate dll anyhow which would be more efficient.

However, that is not to say that this 'dual use' cannot have its uses. For example, you can effectively have an option for the exe to run within the same process as another by simply invoking an appropriate exported function in the exe. Makes it relatively easy to share data between the two programs that way.

I am sure there are many additional reasons for wishing to export EXE functions as well. :)
I may look like a mule, but I'm not a complete ass.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by Kwai chang caine »

Thanks for your explanation. 8)
I'm again too small to use your tips..i have already enough difficulty to use a code when it is not compiled...
Then when it's compiled...

Before it's clear in my litle head...an EXE is alone (A little bit like me with my head)
And A dll is for an EXE...i have print this in my brain

If now you divert to convert DLL in EXE and EXE in DLL....i believe that i begin to be mad :lol: :lol:

It's a pity, you are not in the heaven...because i'm sure you can with your giant knowledge, modify man in women...
Like this ...we have numerous girls computing in this forum :mrgreen:

Too strong my master....too strong !!! 8)

I return to your and Netmaestro code for detecting Mouse ID...because it's already more difficult to me to modify it :cry:
ImageThe happiness is a road...
Not a destination
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by idle »

thanks Srod, good to know that we can export functions without to much sweat.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Export functions in an EXE (just like a dll) - uses coffIT

Post by srod »

14th Feb 2010.

The linker file in the relevant posting above has been adjusted to include the line :

Code: Select all

/FIXED:NO
This option forces the linker to add a relocation .reloc section to the executable. Without this, the Windows loader will not be able to relocate the exe if required.

My thanks to Mistrel for the heads up here.
I may look like a mule, but I'm not a complete ass.
dige
Addict
Addict
Posts: 1410
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: Export functions in an EXE (just like a dll) - uses coff

Post by dige »

fix for win7:

Code: Select all

; Line 17
Case #PB_OS_Windows_XP To #PB_OS_Windows_Future
"Daddy, I'll run faster, then it is not so far..."
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Re: Export functions in an EXE (just like a dll) - uses coff

Post by Seldon »

Thank you for this trick ! It does work here ! Though I've found a problem I don't know if it can be solved. My exported EXE function is like this:

Code: Select all

Structure mystruct
  a.s
  b.s
EndStructure

ProcedureDLL ModifyList(*element.mystruct)
  *element\a="xxx"
  *element\b="yyy"
EndProcedure
I make all the procedure with coffIT and I have a file called 'prova2.exe' .

And this is the caller program:

Code: Select all

Structure mystruct
  a.s
  b.s
EndStructure

Prototype ProtoModifyList(*element.mystruct)

Define *p
Define i

Define ModifyList

NewList mylist.mystruct()

If OpenLibrary(0,"prova2.exe")
  ModifyList.ProtoModifyList=GetFunction(0,"ModifyList")
EndIf

AddElement(mylist())
mylist()\a="ciao"
mylist()\b="mondo"

AddElement(mylist())
mylist()\a="ciao1"
mylist()\b="mondo1"

AddElement(mylist())
mylist()\a="ciao2"
mylist()\b="mondo2"

ForEach mylist()
  *p=mylist()
  If i=1
    ModifyList(*p)
  EndIf
  Debug mylist()\a
  Debug mylist()\b
  Debug ""
  i+1
Next
I get a 'memory write error' when calling the function. That doesn't happen if I use a normal DLL. I've also tried to include the complete PB wrapper, not only the exported object file.

Thanks in advance for any hints.
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Re: Export functions in an EXE (just like a dll) - uses coff

Post by Seldon »

OK, I made further tests and I've found that if I don't use strings, it works :

Code: Select all

Structure mystruct
  a.i
  b.i
EndStructure

ProcedureDLL ModifyList(*element.mystruct)
  *element\a=30
  *element\b=90
EndProcedure
But what if I need strings ? I'll make other tests.
User avatar
mback2k
Enthusiast
Enthusiast
Posts: 257
Joined: Sun Dec 02, 2007 12:11 pm
Location: Germany

Re: Export functions in an EXE (just like a dll) - uses coff

Post by mback2k »

Would it be possible to use coffIT to automatically create debug symbols for all procedures in an executable?

That would be really great to debug Windows services and programs using generic debugging tools.

Corresponding feature request: http://forum.purebasic.com/english/view ... 91&start=0
Post Reply