用 nm 觀察 .so 的 Symbol Table
nm -gC MyLib.so
- -g : 只列出 external symbol
- -C : 將 C++ 的 symbol 轉換成易讀的版本
nm -gC MyLib.so
#include "windows.h"
// Auto dump the life-time of AutoQueryPerformance
// Ex:
// {
// AutoQueryPerformance autoQuery("Test", true);
// ...
// } // it will dump message when @autoQuery destroy
class AutoQueryPerformance
{
public:
AutoQueryPerformance(const char* info, INT loop_count=1, BOOL autoDump=TRUE)
: m_count_time(0), m_loop_count(loop_count), m_auto(autoDump)
{
m_info = info;
::ZeroMemory(&m_cpu_freq, sizeof(LARGE_INTEGER));
::QueryPerformanceFrequency(&m_cpu_freq);
if( autoDump )
begin();
}
~AutoQueryPerformance()
{
pause();
if( autoDump )
printf("[AutoQueryPerformance][%s] spend time=%.4f", m_info.c_str(), getTime());
}
inline void begin()
{
::ZeroMemory(&m_start_clock, sizeof(LARGE_INTEGER));;
::QueryPerformanceCounter(&m_start_clock);
}
inline void pause()
{
::ZeroMemory(&m_cur_clock, sizeof(LARGE_INTEGER));
::QueryPerformanceCounter(&m_cur_clock);
m_count_time += m_cur_clock.QuadPart-m_start_clock.QuadPart;
}
inline void stop()
{
pause();
m_count_time = 0;
}
inline float getTime()
{
return (m_count_time/(float)m_cpu_freq.QuadPart) / m_loop_count;
}
private:
LARGE_INTEGER m_cpu_freq;
LARGE_INTEGER m_start_clock;
LARGE_INTEGER m_cur_clock;
LONGLONG m_count_time;
INT m_loop_count;//for for-loop to calculate correct average time
std::string m_info;
BOOL m_auto;
};
#define AUTO_QUERY_PERFORMANCE(x) AutoQueryPerformance __at_qp = AutoQueryPerformance(x);
void TestingTotalTime(int loop_count)
{
AutoQueryPerformance ap("TestingTotalTime");
int sum = 0;
for(int i=0 ; i<loop_count; i++)
sum += i;
}
void TestingSingleTime(int loop_count)
{
AutoQueryPerformance ap("TestingSingleTime", loop_count);
for(int i=0 ; i<loop_count; i++)
//HeavyFunction call here;
}
// {599D2F5F-BA8D-4009-941C-A5393EBD5C58}
DEFINE_GUID(CLSID_MyCOM,
0x599d2f5f, 0xba8d, 0x4009, 0x94, 0x1c, 0xa5, 0x39, 0x3e, 0xbd, 0x5c, 0x58);
// {4C452CB2-469C-4236-AE5C-F48A28EC0870}
DEFINE_GUID(IID_IMyCOM,
0x4c452cb2, 0x469c, 0x4236, 0xae, 0x5c, 0xf4, 0x8a, 0x28, 0xec, 0x8, 0x70);
HRESULT CreateInstance(IMyCOM** pMyCom)
{
HMODULE hDll = LoadLibrary("MY_COM.dll");//Need refine by your self
if(hDll)
{
//////////////////////////////////////////////////////////////////////////
// Initial COM instance without register
// Try to load COM DLL directly, and call DllGetClassObject to get IID_IClassFactory
// And Create COM instance by IID_IClassFactory
// -----------------------------------------------------------------------
// HRESULT hr = CoCreateInstance(CLSID_MyCOM,
// NULL,
// CLSCTX_INPROC_SERVER,
// IID_IMyCOM,
// (PVOID*)&pMyCom);
//////////////////////////////////////////////////////////////////////////
CComPtr<IClassFactory> spFactory;
// Get address of DllGetClassObject exported method
PDLLGETCLSOBJ pDllGetCLSObj = (PDLLGETCLSOBJ)GetProcAddress(hDll, "DllGetClassObject");
// Call DllGetClassObject and get the proper IClassFactory interface
HRESULT hr = pDllGetCLSObj(CLSID_MyCOM, IID_IClassFactory, reinterpret_cast<PVOID*>(&spFactory));
if( FAILED(hr) )
{
printf(__FUNCTION__" [Err] Can't get spFactory hr=%X", hr);
return E_FAIL;
}
// Create an instance of IMyCOM
hr = spFactory->CreateInstance(NULL, IID_IMyCOM, (PVOID*)pMyCom);
if( FAILED(hr) )
{
printf(__FUNCTION__" [Err] Init IMyCOM Fail! hr=%X", hr);
return E_FAIL;
}
}
else
{
return E_FAIL;
}
return S_OK;
}
import os
import sys
folder = os.path.dirname(os.path.abspath(__file__))
# load JavaLib jna/platform
sys.path.append(os.path.join(folder, 'jna-3.5.1.jar'))
sys.path.append(os.path.join(folder, 'jna-platform-3.5.1.jar'))
# Read
from com.sun.jna.platform.win32 import Advapi32Util, WinReg
path = Advapi32Util.registryGetStringValue( \
WinReg.HKEY_LOCAL_MACHINE,
r'Software\CyberLink\ColorDirector2',
'InstallPath' )
print path.encode(sys.getfilesystemencoding())
bool check_longlong(long long v1)
{
float f = 1.0f;
long long v2 = long long( v1 * f );
printf("ori value=%lld, new value=%lld, ori==new=%d", v1, v2, v1==v2);
return v1==v2;
}
int _tmain(int argc, _TCHAR* argv[])
{
check_longlong(0xF0000000000);//true - OK
check_longlong(0x0FFFFFFFFFF);//false - Error!
check_longlong(0x0FFFFFF);//true - OK
check_longlong(0x1FFFFFF);//false - Error!
return 0;
}
EXTERN_C IMAGE_DOS_HEADER __ImageBase
//...
WCHAR dll_path[MAX_PATH] = {0};
GetModuleFileNameW((HINSTANCE)&__ImageBase, dll_path, _MAX_PATH);