"Fossies" - the Fresh Open Source Software Archive

Member "epstool-3.08/srcwin/wdll.c" (10 Jun 2005, 4626 Bytes) of package /linux/misc/old/ghost/ghostgum/epstool-3.08-os2.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright (C) 2001-2005 Ghostgum Software Pty Ltd.  All rights reserved.
    2 
    3   This software is provided AS-IS with no warranty, either express or
    4   implied.
    5 
    6   This software is distributed under licence and may not be copied,
    7   modified or distributed except as expressly authorised under the terms
    8   of the licence contained in the file LICENCE in this distribution.
    9 
   10   For more information about licensing, please refer to
   11   http://www.ghostgum.com.au/ or contact Ghostsgum Software Pty Ltd, 
   12   218 Gallaghers Rd, Glen Waverley VIC 3150, AUSTRALIA, 
   13   Fax +61 3 9886 6616.
   14 */
   15 
   16 /* $Id: wdll.c,v 1.1.2.6 2005/06/10 09:39:24 ghostgum Exp $ */
   17 
   18 #include <windows.h>
   19 #include <stdio.h>
   20 #include "cplat.h"
   21 #include "cdll.h"
   22 
   23 /******************************************************************/
   24 
   25 static int 
   26 dll_msg(TCHAR *msg, LPCTSTR str, int msglen)
   27 {
   28     int len = (int)cslen(str);
   29     if (len < msglen){
   30     csncat(msg, str, len);
   31     msglen -= len;
   32     }
   33     return msglen;
   34 }
   35 
   36 
   37 #ifndef ERROR_DLL_NOT_FOUND
   38 #define ERROR_DLL_NOT_FOUND 1157L
   39 #endif
   40 
   41 /* display error message for LoadLibrary */
   42 static int
   43 load_library_error(LPCTSTR dllname, TCHAR *msg, int msglen)
   44 {
   45 LPCTSTR text_reason;
   46 TCHAR buf[MAX_PATH+128];
   47 int reason;
   48 LPVOID lpMessageBuffer;
   49     reason = GetLastError() & 0xffff;
   50     switch (reason) {
   51     case ERROR_FILE_NOT_FOUND:  /* 2 */
   52         text_reason = TEXT("File not found");
   53         break;
   54     case ERROR_PATH_NOT_FOUND:  /* 3 */
   55         text_reason = TEXT("Path not found");
   56         break;
   57     case ERROR_NOT_ENOUGH_MEMORY:   /* 8 */
   58         text_reason = TEXT("Not enough memory");
   59         break;
   60     case ERROR_BAD_FORMAT:      /* 11 */
   61         text_reason = TEXT("Bad EXE or DLL format");
   62         break;
   63     case ERROR_OUTOFMEMORY:     /* 14 */
   64         text_reason = TEXT("Out of memory");
   65         break;
   66     case ERROR_DLL_NOT_FOUND:   /* 1157 */
   67         text_reason = TEXT("DLL not found");
   68         break;
   69     default:
   70         text_reason = (TCHAR *)NULL;
   71     }
   72     if (text_reason)
   73         csnprintf(buf, sizeof(buf)/sizeof(TCHAR), 
   74         TEXT("Failed to load %s, error %d = %s\n"), 
   75         dllname, reason, text_reason);
   76     else
   77     csnprintf(buf, sizeof(buf)/sizeof(TCHAR),
   78         TEXT("Failed to load %s, error %d\n"), dllname, reason);
   79     msglen = dll_msg(msg, buf, msglen);
   80 
   81     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
   82     FORMAT_MESSAGE_FROM_SYSTEM,
   83     NULL, reason,
   84     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* user default language */
   85     (LPTSTR) &lpMessageBuffer, 0, NULL);
   86     if (lpMessageBuffer) {
   87         msglen = dll_msg(msg, (LPTSTR)lpMessageBuffer, msglen);
   88         msglen = dll_msg(msg, TEXT("\r\n"), msglen);
   89     LocalFree(LocalHandle(lpMessageBuffer));
   90     }
   91     return msglen;
   92 }
   93 
   94 /* Load DLL.  Write log of actions in msg */
   95 int
   96 dll_open(GGMODULE *hmodule, LPCTSTR name, TCHAR *msg, int msglen)
   97 {
   98 LPCTSTR shortname;
   99 TCHAR fullname[MAX_PATH];
  100 TCHAR *p;
  101 HINSTANCE hInstance = GetModuleHandle(NULL);
  102     memset(msg, 0, msglen);
  103     msglen = dll_msg(msg, TEXT("Trying to load "), msglen);
  104     msglen = dll_msg(msg, name, msglen);
  105     msglen = dll_msg(msg, TEXT("\n"), msglen);
  106 
  107     /* Try to load DLL first with given path */
  108     *hmodule = LoadLibrary(name);
  109     if (*hmodule == (GGMODULE)NULL) {
  110     /* failed */
  111     msglen = load_library_error(name, msg, msglen);
  112     /* try again, with path of EXE */
  113     if ((shortname = csrchr(name, '\\')) == (TCHAR *)NULL)
  114         shortname = name;
  115     else
  116         shortname++;
  117 
  118     GetModuleFileName(hInstance, fullname, sizeof(fullname));
  119     if ((p = csrchr(fullname,'\\')) != (TCHAR *)NULL)
  120         p++;
  121     else
  122         p = fullname;
  123     *p = '\0';
  124     csncat(fullname, shortname, sizeof(fullname)-cslen(fullname)-1);
  125 
  126     msglen = dll_msg(msg, TEXT("Trying to load "), msglen);
  127     msglen = dll_msg(msg, fullname, msglen);
  128     msglen = dll_msg(msg, TEXT("\n"), msglen);
  129 
  130     *hmodule = LoadLibrary(fullname);
  131     if (*hmodule == (GGMODULE)NULL) {
  132         /* failed again */
  133         msglen = load_library_error(fullname, msg, msglen);
  134         /* try once more, this time on system search path */
  135         msglen = dll_msg(msg, TEXT("Trying to load "), msglen);
  136         msglen = dll_msg(msg, shortname, msglen);
  137         msglen = dll_msg(msg, TEXT("\n"), msglen);
  138         *hmodule = LoadLibrary(shortname);
  139         if (*hmodule == (GGMODULE)NULL) {
  140         /* failed again */
  141         msglen = load_library_error(shortname, msg, msglen);
  142         }
  143     }
  144     }
  145 
  146     if (*hmodule == (GGMODULE)NULL) {
  147     *hmodule = (GGMODULE)NULL;
  148     return -1;
  149     }
  150 
  151     return 0;
  152 }
  153 
  154 
  155 int
  156 dll_close(GGMODULE *hmodule)
  157 {
  158     FreeLibrary(*hmodule);
  159     return 0;
  160 }
  161 
  162 
  163 dll_proc
  164 dll_sym(GGMODULE *hmodule, const char *name)
  165 {
  166     return (dll_proc)GetProcAddress(*hmodule, name);
  167 }
  168 
  169 /******************************************************************/