Contiki 3.x
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  */
34 
35 #define WIN32_LEAN_AND_MEAN
36 #define _WIN32_WINNT 0x0501
37 #include <windows.h>
38 
39 #include "contiki.h"
40 
41 /*---------------------------------------------------------------------------*/
42 int
43 dll_loader_load(char *name, char *arg)
44 {
45  HMODULE handle;
46  FARPROC p;
47 
48  /* Load and link the program. */
49  handle = LoadLibrary(name);
50 
51  if(handle == NULL) {
52  debug_printf("dll_loader_load: loading failed: %d\n", GetLastError());
53  return LOADER_ERR_OPEN;
54  }
55 
56  /* Find the processes to be started from the loaded program. */
57  p = GetProcAddress(handle, "autostart_processes");
58  if(p == NULL) {
59  debug_printf("dll_loader_load: could not find symbol 'autostart_processes'\n");
60  return LOADER_ERR_READ;
61  }
62 
63  /* Start the process. */
64  debug_printf("Starting '%s'\n", (**(struct process ***)&p)->name);
65  process_start(**(struct process ***)&p, (void *)arg);
66 
67  return LOADER_OK;
68 }
69 /*---------------------------------------------------------------------------*/
70 void
71 dll_loader_unload(void *addr)
72 {
73  /* Avoid Access Violation Exception due to unloading code still being executed. */
74  QueueUserAPC((PAPCFUNC)dll_loader_unload_dsc, GetCurrentThread(), (ULONG_PTR)addr);
75 }
76 /*---------------------------------------------------------------------------*/
77 struct dsc *
78 dll_loader_load_dsc(char *name)
79 {
80  HMODULE handle;
81  FARPROC d;
82  char symbol[24];
83 
84  handle = LoadLibrary(name);
85 
86  if(handle == NULL) {
87  debug_printf("dll_loader_load_dsc: loading failed: %d\n", GetLastError());
88  return NULL;
89  }
90 
91  strcpy(symbol, name);
92  *strchr(symbol, '.') = '_';
93 
94  d = GetProcAddress(handle, symbol);
95  if(d == NULL) {
96  debug_printf("dll_loader_load_dsc: could not find symbol '%s'\n", symbol);
97  return NULL;
98  }
99 
100  return *(struct dsc **)&d;
101 }
102 /*---------------------------------------------------------------------------*/
103 void __stdcall
104 dll_loader_unload_dsc(void *addr)
105 {
106  HMODULE handle;
107 
108  GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
109  GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, addr, &handle);
110  FreeLibrary(handle);
111 }
112 /*---------------------------------------------------------------------------*/
#define LOADER_OK
No error.
Definition: loader.h:60
#define NULL
The null pointer.
#define LOADER_ERR_READ
Read error.
Definition: loader.h:61
The DSC program description structure.
Definition: dsc.h:75
#define LOADER_ERR_OPEN
Could not open file.
Definition: loader.h:66
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99