File: dll_win.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (133 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
   FALCON - Falcon advanced simple text evaluator.
   FILE: dll_win.cpp

   Implementation of windows specific DLL system
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: mar ago 3 2004

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/


// Must include also windows.h
#include <falcon/dll_win.h>
#include <falcon/memory.h>
#include <falcon/sys.h>
#include <falcon/path.h>

namespace Falcon
{

DllLoader_win::~DllLoader_win()
{
   close();
}

bool DllLoader_win::open( const String &dll_name_fal )
{
   if( m_module != NULL )
      if ( ! FreeLibrary( m_module ) )
         return false;

	String dll_name = dll_name_fal;
   Path::uriToWin( dll_name );

	uint32 bufsize = dll_name.length() * sizeof( wchar_t ) + sizeof( wchar_t );
	wchar_t *dll_name_wc = (wchar_t *) memAlloc( bufsize );
   dll_name.toWideString( dll_name_wc, bufsize );

   m_module = LoadLibraryW( dll_name_wc );

   if ( m_module == NULL ) {
      m_error = GetLastError();
      if (  m_error  == ERROR_CALL_NOT_IMPLEMENTED )
      {
         char *dll_name_c = (char *) dll_name_wc;
         if( dll_name.toCString( dll_name_c, bufsize ) > 0 )
            m_module = LoadLibrary( dll_name_c );
      }
   }

   memFree( dll_name_wc );

   if ( m_module == NULL )
   {
      m_error = GetLastError();
      return false;
   }

   return true;
}

bool DllLoader_win::close()
{
   if ( m_module != NULL ) {
      if ( FreeLibrary( m_module ) ) {
         m_module = NULL;
         return true;
      }
   }
   return false;
}

void DllLoader_win::assign( DllLoader_win &other )
{
   if ( m_module != NULL )
      close();

   m_module = other.m_module;
   other.m_module = NULL;
}


DllFunc DllLoader_win::getSymbol( const char *sym_name ) const
{
   if ( m_module != NULL )
      return DllFunc( (void*)GetProcAddress( m_module, (LPCSTR) sym_name ) );
   return DllFunc( 0 );
}

bool DllLoader_win::isDllMark( char ch1, char ch2 )
{
   if ( ch1 == 'M' && ch2 == 'Z' ) return true;
   return false;
}

void DllLoader_win::getErrorDescription( String &descr ) const
{
   LPVOID lpMsgBuf;

   DWORD res = FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM,
      0,
      m_error,
      LANG_USER_DEFAULT,
      (LPTSTR) &lpMsgBuf,
      0,
      NULL
    );

   if ( res == 0 ) {
      descr = "Impossible to retreive error description";
   }
   else
   {
      descr = (char *) lpMsgBuf;
      // force to copy
      descr.bufferize();
   }

   LocalFree(lpMsgBuf);
}


}


/* end of dll_win.cpp */