# examples/PIPS/HelloWorldExample/src/helloworldexeexample/helloworldexeexample.c

    00001 // helloworldexeexample.c
    00002 //
    00003 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
    00004 // All rights reserved.
    00005 // This component and the accompanying materials are made available
    00006 // under the terms of "Eclipse Public License v1.0"
    00007 // which accompanies this distribution, and is available
    00008 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    00009 //
    00010 // Initial Contributors:
    00011 // Nokia Corporation - initial contribution.
    00012 //
    00013 // Contributors:
    00014 //
    00015 // Description:
    00016 //
    00017
    00018
    00019 /* This example is a simple PIPS STDEXE application. It loads the DLL and calls its exported function.
    00020 The application implements the dynamic lookup by name mechanism of the exported function using the libdl APIs.
    00021 */
    00022
    00023 // Include files.
    00024 #include <stdio.h>
    00025 #include <dlfcn.h>
    00026 #include <stdlib.h>
    00027
    00031 void PressAKey()
    00032         {
    00033         fflush(stdout);
    00034         getchar();
    00035         }
    00040 int DisplayErrorMessage()
    00041         {
    00042         // Obtain the last error occurred using the libdl API, dlerror().
    00043         const char *q = dlerror();
    00044
    00045         // Prints the error message.
    00046         printf("%s",q);
    00047         PressAKey();
    00048
    00049         // returns the failure code.
    00050         return EXIT_FAILURE;
    00051         }
    00052
    00053 int main()
    00054 {
    00055         int result;
    00056
    00057         // Handle to load the dynamic library.
    00058         void* handleToDll;
    00059
    00060         typedef void (*PrintHelloWorldFunction)(void);
    00061         PrintHelloWorldFunction PrintHelloWorld;
    00062
    00063         // Contains name of the DLL file.
    00064         char dllFileName[] = "helloworlddllexample.dll";
    00065
    00071         handleToDll = dlopen(dllFileName, RTLD_LAZY);
    00072
    00073         // Check the descriptor value.If it is null, the error message is printed.
    00074         if (!handleToDll)
    00075                 {
    00076                 DisplayErrorMessage();
    00077                 }
    00078
    00079         // Print the message to start the example application.
    00080         printf(" Press enter key to step through the example application\n");
    00081         PressAKey();
    00082
    00088         PrintHelloWorld = (PrintHelloWorldFunction)dlsym(handleToDll, "PrintHelloWorld");
    00089
    00090         // Check the symbol address value. If this is null, the error message is printed.
    00091         if(!PrintHelloWorld)
    00092                 {
    00093                 DisplayErrorMessage();
    00094                 }
    00095
    00096         // Call the loaded DLL's exported function.
    00097         PrintHelloWorld();
    00098
    00105         result= dlclose(handleToDll);
    00106
    00107         // Check the return value. If this is -1, the error message is printed.
    00108         if(result == -1)
    00109                 {
    00110                 DisplayErrorMessage();
    00111                 }
    00112
    00113         // Print the message to exit from the example application.
    00114         printf(" Press enter key to exit from the example application");
    00115         PressAKey();
    00116
    00117         // returns the success code.
    00118         return EXIT_SUCCESS;
    00119 }
    00120
    00121
    00122