- clr中new delete导致异常, 可以使用静态对象
CSharpProxy * CSharpProxy::Instance()
{
static CSharpProxy s_Obj;
if(g_pSharpProxy == NULL)
{
#if(1)
g_pSharpProxy = &s_Obj;
#else
g_pSharpProxy = new CSharpProxy(); // clr中new delete导致异常
atexit(DeleteAction);
#endif
g_pSharpProxy->Init();
}
return g_pSharpProxy;
}
- 引用的dll在指定路径
static Assembly^ ResolvePath(Object^ sender, ResolveEventArgs^ args)
{
Assembly ^ret;
String ^dll_name;
String ^dll_dir;
String ^dll_Path;
AssemblyName^ AsmName;
CHAR *pName, *pRsvPath, *pResult;
AsmName = gcnew AssemblyName(args->Name);
dll_name = AsmName->Name;
pName = (CHAR*)(void*)InteropServices::Marshal::StringToHGlobalAnsi(dll_name);
if(dll_name->IndexOf(".resources") >= 0)
{
ATLTRACE("ResolvePath stop: %s\r\n", pName);
return nullptr;
}
dll_dir = gcnew String(s_DllPath);
dll_Path = dll_dir + "\\" + dll_name + ".dll";
ret = Assembly::LoadFrom(dll_Path);
pResult = ret ? "OK" : "FAIL";
pRsvPath = (CHAR*)(void*)InteropServices::Marshal::StringToHGlobalAnsi(dll_Path);
ATLTRACE("ResolvePath %4s: %s -> %s\r\n", pResult, pName, pRsvPath);
return ret;
}
AppDomain::CurrentDomain->AssemblyResolve += gcnew ResolveEventHandler(ResolvePath);
- String 对象转换
BOOL GetStringText(String^ _Value, string &sText)
{
CHAR *pResult;
if(String::IsNullOrEmpty(_Value))
{
sText = "";
return TRUE;
}
pResult = (CHAR*)(void*)InteropServices::Marshal::StringToHGlobalAnsi(_Value);
sText = pResult;
InteropServices::Marshal::FreeHGlobal(IntPtr(pResult));
return TRUE;
}
String^ SetStringText(string &sText)
{
return gcnew String(sText.data());
}
- Dictionary
BOOL Dictionary2Map(Dictionary<String^, String^> ^dict, map<std::string, std::string> &MapDat)
{
List<String^> keylist = gcnew List<String^>(dict->Keys);
String ^Key, ^Value;
string sName, sValue;
int i;
MapDat.clear();
for(i=0; i<keylist.Count; i++)
{
Key = keylist[i];
Value = dict[Key];
GetStringText(Key, sName);
GetStringText(Value, sValue);
MapDat[sName] = sValue;
}
return TRUE;
}
/*
VS2010当前CLR不支持 foreach
使用 foreach 内部核心实现(反编译foreach)
*/
BOOL Dictionary2Map2(Dictionary<String^, String^> ^dict, map<std::string, std::string> &MapDat)
{
Dictionary<String^, String^>::Enumerator ^_em;
KeyValuePair<String ^, String ^> ^_Cur;
String ^_Key, ^_Value;
string sName, sValue;
for(_em = dict->GetEnumerator();
_em->MoveNext();
)
{
_Cur = _em->Current;
_Key = _Cur->Key;
_Value = _Cur->Value;
GetStringText(_Key, sName);
GetStringText(_Value, sValue);
MapDat[sName] = sValue;
}
return TRUE;
}
- String []
// String []
array<String^> ^CreateArrayString(int nSize)
{
array<String^> ^_ary = gcnew array<String^>(4);
return _ary;
}
// String[] Clr
array<String^> ^CreateArrayString(list<string> &strList)
{
list<string>::iterator Iter;
array<String^> ^_ary = gcnew array<String^>(strList.size());
int i;
i = 0;
for(Iter = strList.begin();
Iter != strList.end();
Iter ++)
{
_ary[i] = gcnew String(Iter->data());
i ++;
}
return _ary;
}
// String[] C#
Array ^CreateStringArray(list<string> &strList)
{
array<String^> ^_ary;
_ary = CreateArrayString(strList);
return _ary;
}
BOOL GetClrList(System::Array ^_ary, list<string> &DatList)
{
System::Object ^_obj;
String ^_item;
int i, m;
string sText;
DatList.clear();
m = _ary->GetLength(0);
for(i=0; i<m; i++)
{
_obj = _ary->GetValue(i);
_item = _obj->ToString();
GetStringText(_item, sText);
DatList.push_back(sText);
}
return TRUE;
}
- 静态对象避免重复创建
TestingBLL ^Instance_TestingBLL()
{
static gcroot<TestingBLL^> *objPtr = new gcroot<TestingBLL^>;
static BOOL s_Init = FALSE;
if(s_Init == FALSE)
{
s_Init = TRUE;
*objPtr = gcnew TestingBLL();
}
return *objPtr;
}
- 根据名称设置对象属性值
BOOL SetObjProp(System::Object ^_obj, map<string,string> &MapDat)
{
map<string,string>::iterator Iter;
System::Type ^_type;
PropertyInfo ^_prop_info;
System::Object ^_prop_value;
String ^_prop_name;
string sName, sValue;
BOOL bResult;
int nValue;
bResult = TRUE;
_type = _obj->GetType();
for(Iter = MapDat.begin();
Iter != MapDat.end();
Iter ++)
{
sName = Iter->first;
sValue = Iter->second;
_prop_name = gcnew String(sName.data());
_prop_info = _type->GetProperty(_prop_name);
if(_prop_info == nullptr)
{
assert(0);
bResult = FALSE;
continue;
}
if(_prop_info->PropertyType->Name == "String")
{
_prop_value = gcnew String(sValue.data());
_prop_info->SetValue(_obj, _prop_value, nullptr);
}
else if(_prop_info->PropertyType->Name == "Int32")
{
nValue = atoi(sValue.data());
_prop_value = gcnew Int32(nValue);
_prop_info->SetValue(_obj, _prop_value, nullptr);
}
else
{
assert(0);
bResult = FALSE;
continue;
}
}
return bResult;
}