Contiki 2.5
dll-loader.c
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Oliver Schmidt <ol.sc@web.de>
32  *
33  * $Id: dll-loader.c,v 1.4 2008/02/07 15:54:50 oliverschmidt Exp $
34  */
35 
36 #define WIN32_LEAN_AND_MEAN
37 #define _WIN32_WINNT 0x0501
38 #include <windows.h>
39 
40 #include "contiki.h"
41 
42 /*---------------------------------------------------------------------------*/
43 int
44 dll_loader_load(char *name, char *arg)
45 {
46  HMODULE handle;
47  FARPROC p;
48 
49  /* Load and link the program. */
50  handle = LoadLibrary(name);
51 
52  if(handle == NULL) {
53  debug_printf("dll_loader_load: loading failed: %d\n", GetLastError());
54  return LOADER_ERR_OPEN;
55  }
56 
57  /* Find the processes to be started from the loaded program. */
58  p = GetProcAddress(handle, "autostart_processes");
59  if(p == NULL) {
60  debug_printf("dll_loader_load: could not find symbol 'autostart_processes'\n");
61  return LOADER_ERR_READ;
62  }
63 
64  /* Start the process. */
65  debug_printf("Starting '%s'\n", (**(struct process ***)&p)->name);
66  process_start(**(struct process ***)&p, arg);
67 
68  return LOADER_OK;
69 }
70 /*---------------------------------------------------------------------------*/
71 void
72 dll_loader_unload(void *addr)
73 {
74  /* Avoid Access Violation Exception due to unloading code still being executed. */
75  QueueUserAPC((PAPCFUNC)dll_loader_unload_dsc, GetCurrentThread(), (ULONG_PTR)addr);
76 }
77 /*---------------------------------------------------------------------------*/
78 struct dsc *
79 dll_loader_load_dsc(char *name)
80 {
81  HMODULE handle;
82  FARPROC d;
83  char symbol[24];
84 
85  handle = LoadLibrary(name);
86 
87  if(handle == NULL) {
88  debug_printf("dll_loader_load_dsc: loading failed: %d\n", GetLastError());
89  return NULL;
90  }
91 
92  strcpy(symbol, name);
93  *strchr(symbol, '.') = '_';
94 
95  d = GetProcAddress(handle, symbol);
96  if(d == NULL) {
97  debug_printf("dll_loader_load_dsc: could not find symbol '%s'\n", symbol);
98  return NULL;
99  }
100 
101  return *(struct dsc **)&d;
102 }
103 /*---------------------------------------------------------------------------*/
104 void __stdcall
105 dll_loader_unload_dsc(void *addr)
106 {
107  HMODULE handle;
108 
109  GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
110  GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, addr, &handle);
111  FreeLibrary(handle);
112 }
113 /*---------------------------------------------------------------------------*/