Delphi調用c++的DLL |
尚未結案
|
frog1
一般會員 發表:25 回覆:40 積分:18 註冊:2007-01-24 發送簡訊給我 |
Hi, 各位前輩
我用C 寫了一個DLL, 在C 上調用沒問題, 但不能在上Delphi調用, 下列為部份程式碼 有找過資料, 了解, C 寫給其他語言調用應使用stdcall, 但我把cdecl改用stdcall, delphi一進入就出錯, 但用cdecl, C 接到的傳址參數位置竟一直在變動, 所以第一次有得到正確的資料, 以後因C DLL所放的資料位置與delphi不同, 所以我一直取到的是舊的資料, 圖像都不會變動 懇 請各位前輩指點, 如何才可讓兩者的傳址參數一致 謝 MainDll.cpp [code cpp] extern "C" __declspec(dllexport) int __cdecl getImageData(unsigned char* imgData) { memcpy(imgData, img->imageData, img->imageSize); unsigned char *p = imgData; //------------------- char asd[10]; // 這四行是測試捉 imgData的記憶體位址 sprintf(asd,"%x",&p); //C 執行的結果正確, 而imgData的記憶體位址, 固定不變 MessageBox(0, asd, "DLL Message", MB_OK | MB_ICONINFORMATION); //delphi執行結果, 因imgData的記憶體位址一直不同, 所以在顯示時, 圖像都不會改變 int intGetImg = 0; for(int i=0; i<10; i ){ intGetImg = getImage(img); if (intGetImg) break; Sleep(10); } return intGetImg; } [/code] MainDll.h [code cpp] #define DLL_EXPORT __declspec(dllexport) #ifdef BUILD_DLL #define DLL_EXPORT __declspec(dllexport) #else #define DLL_EXPORT __declspec(dllimport) #endif #ifdef __cplusplus extern "C" { #endif extern "C" __declspec(dllexport) int __cdecl getImageData(unsigned char* imgData); #ifdef __cplusplus } #endif [/code] 調用的Delphi [code delphi] implementation Function dllGetImage(var chrdata:word):integer; cdecl; external 'carLicense_dll.dll' name 'getImageData'; procedure TForm1.Timer1Timer(Sender: TObject); begin dllGetImage(chrdata[0]); //實際調用DLL函數 mstbmp.Write(bmfh, SizeOf(bmfh)); mstbmp.Write(bmih, SizeOf(bmih)); mstbmp.Write(chrdata[0], w*h*3); mstbmp.Position:=0; bmp.LoadFromStream(mstbmp); Image1.Picture.Bitmap.Assign(bmp); image1.Update; bmp.SaveToFile('d:\sss.bmp'); inc(int); edit1.Text := inttostr(int); end; procedure TForm1.FormCreate(Sender: TObject); begin w := 1600; h := 1200; bmfh.bfType := $4d42; //'BM' bmfh.bfSize := SizeOf(bmfh) SizeOf(bmih) W * H *3; bmfh.bfReserved1 := 0; bmfh.bfReserved2 := 0; bmfh.bfOffBits := SizeOf(bmfh) SizeOf(bmih); // bytes of BitMapInfoHeader bmih.biSize := SizeOf(bmih); bmih.biWidth := W; bmih.biHeight := (-1)*H; // (-1)*H顯示時可自動上下顛倒 bmih.biPlanes := 1; bmih.biBitCount := 24; bmih.biCompression := 0; bmih.biSizeImage := 0; bmih.biXPelsPerMeter := 72; bmih.biYPelsPerMeter := 72; bmih.biClrUsed := 0; bmih.biClrImportant := 0; mst := TMemoryStream.Create; mst.SetSize(w*h*3); mstbmp := TMemoryStream.Create; bmp:= TBitMap.Create; GetMem(chrdata,w*h*3); end; [/code] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |