Skip to content

Commit

Permalink
add dll class
Browse files Browse the repository at this point in the history
  • Loading branch information
vintagewang committed Jun 10, 2012
1 parent af10fdd commit 02424f1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/jwrapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<classpaths>
<path>cp1</path>
<path>cp2</path>
<path>${cwd}/../lib</path>
<jars>cp3</jars>
</classpaths>

Expand Down
57 changes: 57 additions & 0 deletions src/DLL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2012 Wangxr, [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "DLL.h"
#include <assert.h>

DLL::DLL(const char* file) : dll(NULL)
{
assert(NULL != file);

#ifdef
this->dll = ::LoadLibraryA(file);
#else
this->dll = dlopen(file, RTLD_NOW + RTLD_GLOBAL);
if(NULL == this->dll) {
fprintf(stderr, "dllopen error %s\n", dlerror());
}
#endif
}

DLL::~DLL()
{
if(NULL != this->dll) {
#ifdef
::FreeLibrary(this->dll);
#else
::dlclose(this->dll);
#endif
}
}

void* DLL::getFunction(const char* functionName)
{
assert(NULL != functionName);

if(NULL != this->dll) {
#ifdef WIN32
return (void*)::GetProcAddress(this->dll, functionName);
#else
return (void*)::dlsym(this->dll, functionName);
#endif
}

return NULL;
}
37 changes: 37 additions & 0 deletions src/DLL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2012 Wangxr, [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _JW_DLL_H__
#define _JW_DLL_H__
#ifdef WIN32
#include <Windows.h>
#include <WinBase.h>
typedef HINSTANCE DLL_T;
#else
#include <dlfcn.h>
typedef void* DLL_T;
#endif

class DLL
{
DLL_T dll;

public:
DLL(const char* file);
~DLL();
void* getFunction(const char* functionName);
};

#endif // end of _JW_DLL_H__

0 comments on commit 02424f1

Please sign in to comment.