Linux or UINX dlopen 사용예제
프로그래밍 2011. 3. 15. 11:36 |==== test.h
#if !defined(__TEST_H__)
#define __TEST_H__
#include <gtk/gtk.h>
void create_dlg ( GtkButton *button, GtkWindow *parent ) ;
#endif /* __TEST_H__ */
==== test.c
// cc -shared -Wall -g `pkg-config --cflags --libs gtk+-2.0` test.c -o libtest.so
#include <gtk/gtk.h>
#include "test.h"
void create_dlg (GtkButton *button, GtkWindow *parent)
{
GtkWidget *dialog, *label, *image, *hbox;
/* Create a new dialog with one OK button. */
dialog = gtk_dialog_new_with_buttons ("Information", parent,
GTK_DIALOG_MODAL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
label = gtk_label_new ("The button was clicked!");
image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_INFO,
GTK_ICON_SIZE_DIALOG);
hbox = gtk_hbox_new (FALSE, 5);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
gtk_box_pack_start_defaults (GTK_BOX (hbox), image);
gtk_box_pack_start_defaults (GTK_BOX (hbox), label);
/* Pack the dialog content into the dialog's GtkVBox. */
gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox);
gtk_widget_show_all (dialog);
/* Create the dialog as modal and destroy it when a button is pressed. */
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
==== dialogs.c
// # cc -Wall -g `pkg-config --cflags --libs gtk+-2.0` dialogs.c -o dialogs -ldl
// 실행전 # export LD_LIBRARY_PATH=`pwd`
//
#include <gtk/gtk.h>
#include <dlfcn.h>
void *FunctionLib; /* Handle to shared lib file */
int (*Function)(GtkButton * , GtkWindow * ); /* Pointer to loaded routine */
const char *dlError; /* Pointer to error string */
static void button_clicked (GtkButton*, GtkWindow*);
int main (int argc,
char *argv[])
{
GtkWidget *window, *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Dialogs");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
button = gtk_button_new_with_mnemonic ("_Click Me");
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (button_clicked),
(gpointer) window);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
/* Create a new GtkDialog that will tell the user that the button was clicked. */
static void
button_clicked (GtkButton *button,
GtkWindow *parent)
{
int rc ;
FunctionLib = dlopen("libtest.so",RTLD_LAZY);
dlError = dlerror();
printf(" Test 3-Open Library with absolute path return-%s- \n", dlError);
if( dlError ) exit(1);
Function = dlsym( FunctionLib, "create_dlg");
dlError = dlerror();
printf(" dlTest 8-Find symbol printLowercase return-%s- \n", dlError);
if( dlError ) exit(1);
rc = (*Function)( button , parent );
printf(" dlTest 9-printLowercase return-%s- \n", dlError);
rc = dlclose(FunctionLib);
dlError = dlerror();
printf(" dlTest 10-Close handle return-%s-\n",dlError);
if( rc ) exit(1);
}
'프로그래밍' 카테고리의 다른 글
MFC BITMAP 구조체 (0) | 2011.06.17 |
---|---|
안드로이드 시스템 rw로 마운트하기 (0) | 2011.05.03 |
C++ 구조체 포인터의 값을 C# 구조체 배열로 가져오기 (0) | 2011.03.14 |
vc++ 호출 규약 (0) | 2011.03.14 |
C# C에서 쓰던 typedef 및 #define 사용하기 (0) | 2011.03.14 |