嚐試不同使用動態模組(.so or .dll)的方法
[sean_bcd.h]
#if defined(__cplusplus)
#if defined(_MSC_VER)
#define HANDLE_PRAGMA_PACK_PUSH_POP //WIN32 style pack
#define X_FASTCALL
#define X_IMPORT __declspec(dllimport)
#define X_EXPORT __declspec(dllexport)
#elif defined(__BORLANDC__)
#define HANDLE_PRAGMA_PACK_PUSH_POP //WIN32 style pack
#define X_FASTCALL __fastcall
#define X_FASTCALL_CTOR X_FASTCALL
#define X_IMPORT __declspec(package)
#define X_EXPORT __declspec(package)
#else
#define X_IMPORT
#define X_EXPORT
#define X_FASTCALL
#define X_FASTCALL_CTOR
#endif
#if defined(SEAN_DLL_IMPL)
#define SEAN_CLASS_DECL X_IMPORT
//#pragma comment(lib, "<DLL ...>.lib")
#define SEAN_CLASS_DECL X_EXPORT
class SEAN_CLASS_DECL sean_bcd
{
public:
int bcd2num( char *out, int outsz, char *in, int inlen );
char asc2bin( char c );
};
extern "C" {
#endif
int bcd2num_( char *out, int outsz, char *in, int inlen );
#if defined(__cplusplus)
}
#endif
[sean_bcd.cpp]
#include "sean_bcd.h"
#include <ctype.h>
char sean_bcd::asc2bin( char c )
{
if ( isdigit( c ) ) return( c - '0' );
if ( isalpha( c ) )
{
c = toupper( c );
return( c - 'A' + 10 );
}
else
return 0;
}
int sean_bcd::bcd2num( char *out, int outsz, char *in, int inlen )
{
int i,j;
int limit = outsz*2;
int left=limit-inlen;
char c, c1;
for ( i=0,j=0; i<limit ; i+=2 )
{
if(left > 0)
{
c = 0;
left--;
}
else
{
c = asc2bin( *( in + j ) );
j++;
}
c1 = ( c << 4 ) & 0xf0;
if(left > 0)
{
c = 0;
left--;
}
else
{
c = asc2bin( *( in + j ) );
j++;
}
*( out + i/2 ) = c | c1;
}
return i;
}
[make so file]
g++ -shared -fPIC -o libbcd.so sean_bcd.cpp
BTW. (c file make to so file)
gcc -shared -fPIC -o libbcd.so sean_bcd.c
[c with make option]
vi sean_c_op.c
#include <stdio.h>
#include "./sean_bcd.h"
int main(int argc , char *argv[])
{
unsigned char buf[10];
int i;
if(argc < 2)
{
printf("give a bcd parameter !!\n");
return 1;
}
bcd2num_(buf,sizeof(buf),argv[1],strlen(argv[1]));
printf("bcd[%s] to number[",argv[1]);
for ( i = 0 ; i<sizeof(buf) ; i++)
{
printf("%02X",buf[i]);
}
printf("]\n");
return 0;
}
gcc -o sean_c_op sean_c_op.c -lbcd -L.
[root@localhost ~]# export LD_LIBRARY_PATH=.;./sean_c_op "123456789012345"
bcd[123456789012345] to number[00000123456789012345]
[c with dlopen]
vi sean_c_dlopen.c
#include <stdio.h>
#include "./sean_bcd.h"
#include <dlfcn.h>
int (*sean_bcd_dl)(char *out, int outsz, char *in, int inlen);
#define LoadLibrary(path) dlopen(path,RTLD_NOW)
#define FreeLibrary(handle) dlclose(handle)
#define GetProcAddress(h,sym) dlsym(h,sym)
void * attach_bcblib(const char *path)
{
void *handle = dlopen(path, RTLD_NOW);
// void *handle = LoadLibrary(path);
sean_bcd_dl = dlsym(handle, "bcd2num_");
// sean_bcd_dl = GetProcAddress(handle, "bcd2num_");
return handle;
}
void detach_bcblib(void* handle)
{
if(handle)
{
dlclose(handle);
// FreeLibrary(handle);
handle=NULL;
}
}
int main(int argc , char *argv[])
{
unsigned char buf[10];
void *handle=NULL;
int i;
if(argc < 2)
{
printf("give a bcd parameter !!\n");
return 1;
}
handle = attach_bcblib("./libbcd.so");
sean_bcd_dl(buf,sizeof(buf),argv[1],strlen(argv[1]));
printf("bcd[%s] to number[",argv[1]);
for ( i = 0 ; i<sizeof(buf) ; i++)
{
printf("%02X",buf[i]);
}
printf("]\n");
detach_bcblib(handle);
return 0;
}
gcc -o sean_c_dlopen sean_c_dlopen.c -ldl
[root@localhost ~]# export LD_LIBRARY_PATH=.;./sean_c_dlopen "123456789012345"
bcd[123456789012345] to number[00000123456789012345]
[cpp]
vi sean_cpp.cpp
#include <stdio.h>
#include <string.h>
#include "./sean_bcd.h"
int main(int argc , char *argv[])
{
unsigned char buf[10];
sean_bcd seanobj;
int i;
if(argc < 2)
{
printf("give a bcd parameter !!\n");
return 1;
}
seanobj.bcd2num((char*)buf,sizeof(buf),argv[1],strlen(argv[1]));
printf("bcd[%s] to number[",argv[1]);
for ( i = 0 ; i<sizeof(buf) ; i++)
{
printf("%02X",buf[i]);
}
printf("]\n");
return 0;
}
g++ -o sean_cpp sean_cpp.cpp -lbcd -L. -DSEAN_DLL_IMPL
[root@localhost ~]# export LD_LIBRARY_PATH=.;./sean_cpp "123456789012345"
bcd[123456789012345] to number[00000123456789012345]
沒有留言:
張貼留言