我正在試圖在C++/CLI中使用帶有/clr
模式的Berkeley DB。我寫了這個代碼:與Berkeley DB混合的C++/CLI代碼
編輯:
// DB_test1.cpp : main project file.
#include "stdafx.h"
#pragma comment(lib,"libdb51")
using namespace System;
using namespace System::Runtime::InteropServices;
int main(array<System::String ^> ^args)
{
Db SigDb(0,0);
unsigned int oFlags= DB_CREATE;
SigDb.open(NULL,"SigDb.db",0,DB_BTREE,oFlags,0);
String^ HexSig="D8B1048900ABFF8B";
wchar_t* a=(wchar_t*)Marshal::StringToHGlobalUni(HexSig).ToPointer() ;
wchar_t* A=(wchar_t*)Marshal::StringToHGlobalUni(HexSig).ToPointer();;
Dbt key1(&a,100);
Dbt data1(&A,100);
Marshal::FreeHGlobal(IntPtr(A));
int ret= SigDb.put(NULL,&key1,&data1, DB_NOOVERWRITE);
if(ret==DB_KEYEXIST){
Console::WriteLine("You are trying to insert an exist key!");
}
wchar_t DDData[200];
Dbt getKey, getData;
getKey.set_data(&a);
getKey.set_size(100);
getData.set_data(DDData);
getData.set_ulen(200);
getData.set_flags(DB_DBT_USERMEM);
Marshal::FreeHGlobal(IntPtr(a));
if(SigDb.get(NULL,&getKey,&getData,0)==DB_NOTFOUND)
Console::WriteLine("Not Found !");
else
Console::WriteLine(" {0}",Marshal::PtrToStringUni((IntPtr)DDData));
return 0;
}
的代碼編譯成功,但它顯示了錯誤的輸出。我只是traying存儲String^ HexSig="D8B1048900ABFF8B";
在SigDb.db
,然後直接讀取相同的字符串,並打印它!結果不像預期的那樣出現D8B1048900ABFF8B
,但它顯示爲隨機字符串。有任何想法嗎?
編輯後: 這個代碼段始終執行Console::WriteLine("Not Found !");
非常感謝Alex!這個問題已經通過你的筆記解決了! – Aan 2011-04-22 16:45:12