export function from the executable

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

export function from the executable

Post by hallodri »

hi,

I use the GtkBuilder with Glade, where you can specify an event function and it will later call it.

Example in C:

Code: Select all

#include <gtk\gtk.h>

extern "C" __declspec( dllexport ) void on_window1_destroy()
{
	gtk_main_quit();
}

int main(int argc, char** argv)
{

	GtkWidget	*Window = 0;
	GtkBuilder	*GBuild;

	gtk_init(&argc, &argv);

	
	GBuild = gtk_builder_new();
	
	gtk_builder_add_from_file(GBuild, "test2.glade", NULL);
	
	Window = GTK_WIDGET(gtk_builder_get_object (GBuild, "window1"));

	gtk_builder_connect_signals(GBuild, 0);

	gtk_widget_show_all(Window);

	g_object_unref (GBuild);

	gtk_main();

	return 0;
}
test2.glade

Code: Select all

<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window1">
    <signal name="destroy" handler="on_window1_destroy"/>
  </object>
</interface>
When I close the window is automatically invoked on_window1_destroy.

Example PB:

Code: Select all

XIncludeFile "..\include\gtk2.pbi"

ProcedureCDLL on_window1_destroy()
  gtk_main_quit()
EndProcedure

ProcedureC main()  
  Protected window.i
  Protected gBuilder.i
  
  
  If gtk_init(0, 0)
    
    gBuilder = gtk_builder_new ()        
    
    gtk_builder_add_from_file(gBuilder, @"test2.glade", #Null)            
    gtk_builder_connect_signals(gBuilder, 0)
    
  	Window = gtk_builder_get_object (gBuilder, @"window1")
  	gtk_widget_show_all(Window)   
    
    g_object_unref (gBuilder)
    
    gtk_main ()
                   
  EndIf

EndProcedure

main() 

With Purebasic the whole does not work, because PB is not the function exported.
Therefore, my wish that Purebasic functions declared with ProcedureCDLL / ProcedureDLL
be exported from executable.
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: export function from the executable

Post by uwekel »

Hi!

I am stuck on the same problem right now. Is it already possible with PureBasic?

As a work around, you can use

Code: Select all

g_signal_connect_(*myobject, "signal-name", @my_handler(), user_data)
but it's a line for any single event you want to track.

Best regards
Uwe.
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: export function from the executable

Post by idle »

Uwe

As it happens you can export a function reasonably easily if you view the commented ASM to find the label of the function
in your finished code.

http://www.purebasic.fr/english/viewtop ... 12&t=52807
Windows 11, Manjaro, Raspberry Pi OS
Image
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: export function from the executable

Post by User_Russian »

idle, does not work exported function.

Code: Select all

Import "/EXPORT:Message" : EndImport 
;...
!public _Procedure0 As 'Message'
ProcedureDLL Message(Title.s, Message.s)
  MessageRequester(Title, Message)
EndProcedure

Code: Select all

Prototype Funct(Title.s, Message.s)

If OpenLibrary(0,"exe.exe")
  Funct.Funct=GetFunction(0, "Message")
  If Funct
    Funct("Message", "Test")
  EndIf
EndIf
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Re: export function from the executable

Post by hallodri »

try this

Code: Select all

ProcedureDLL Message(Title.s, Message.s)
	MessageRequester(Title, Message)
	ProcedureReturn 
	Message("", "") ; <--- important
EndProcedure
PB puts the function in a macro. If the macro is never called, the code will not compile.
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: export function from the executable

Post by idle »

@User_Russian

I think you need to own the processes to call an exported function from it.

The trick works for gtk_builder_connect_signals(GBuild, 0);
because gtk is loaded into your process and it can then use library functions
to find and set the callbacks.
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply