第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問為什么不能將WideString用作互操作的函數(shù)返回值?

請問為什么不能將WideString用作互操作的函數(shù)返回值?

aluckdog 2019-11-05 04:04:13
為什么不能將WideString用作互操作的函數(shù)返回值?我不止一次建議人們使用類型的返回值。WideString為了互操作的目的。訪問DelphiDLL拋出ocasional異常ASP.NET Web應(yīng)用程序在IIS Web服務(wù)器上調(diào)用DelphiDLL,返回Pchar字符串時鎖定為什么DelphiDLL可以在不使用ShareMem的情況下使用WideString?我的想法是WideString與BSTR..因?yàn)锽STR在共享COM堆上分配,那么在一個模塊中分配并在另一個模塊中釋放是沒有問題的。這是因?yàn)樗懈鞣蕉纪馐褂孟嗤亩?,COM堆。然而,似乎WideString不能用作互操作的函數(shù)返回值。考慮下面的DelphiDLL。library WideStringTest;uses   ActiveX;function TestWideString: WideString; stdcall;begin   Result := 'TestWideString';end;function TestBSTR: TBstr; stdcall;begin   Result := SysAllocString('TestBSTR');end;procedure TestWideStringOutParam(out str: WideString); stdcall;begin   str := 'TestWideStringOutParam';end;exports   TestWideString, TestBSTR, TestWideStringOutParam;beginend.以及下面的C+代碼:typedef BSTR (__stdcall *Func)();typedef void (__stdcall *OutParam)(BSTR &pstr);HMODULE lib = LoadLibrary(DLLNAME);Func TestWideString =  (Func) GetProcAddress(lib, "TestWideString");Func TestBSTR = (Func) GetProcAddress(lib, "TestBSTR");OutParam TestWideStringOutParam = (Out Param) GetProcAddress(lib,                    "TestWideStringOutParam");BSTR str = TestBSTR();wprintf(L"%s\n", str);SysFreeString(str);str = NULL;TestWideSt                    ringOutParam(str);wprintf(L"%s\n", str);SysFreeString(str);str = NULL;str = TestWideString();                    //fails herewprintf(L"%s\n", str);SysFreeString(str);打電話給TestWideString此錯誤失?。築STRtest.exe中0x772015de處的未處理異常:0xC0000005:訪問沖突讀取位置0x00000000。類似地,如果我們嘗試用p/Invoke從C#調(diào)用它,則會有一個失?。篬DllImport(@"path\to\my\dll")][return: MarshalAs(UnmanagedType.BStr)]static extern string TestWideString();錯誤是:ConsoleApplication10.exe中發(fā)生了“System.Runtime.InteropServices.SEHException”類型的未處理異常附加信息:外部組件引發(fā)異常。呼叫TestWideStringVia/Invoke按預(yù)期工作。因此,使用WideString參數(shù)按引用傳遞,并將它們映射到BSTR似乎運(yùn)作得很好。但不適用于函數(shù)返回值。我已經(jīng)在Delphi 5,2010和XE2上測試了這一點(diǎn),并且在所有版本上都觀察到了相同的行為。執(zhí)行進(jìn)入Delphi,幾乎立即失敗。分配給Result變成打電話到System._WStrAsg,第一行內(nèi)容如下:CMP     [EAX],EDX現(xiàn)在,EAX是$00000000當(dāng)然會有訪問違規(guī)行為。有人能解釋一下嗎?我做錯什么了嗎?我的期望是不是不合理?WideString函數(shù)值是可行的BSTR是嗎?還是僅僅是德爾菲的缺陷?
查看完整描述

3 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個贊

在常規(guī)的Delphi函數(shù)中,函數(shù)返回實(shí)際上是一個通過引用傳遞的參數(shù),即使在語法上它看起來和感覺都像是一個“Out”參數(shù)。您可以這樣測試它(這可能取決于版本):

function DoNothing: IInterface;begin
  if Assigned(Result) then
    ShowMessage('result assigned before invocation')
  else
    ShowMessage('result NOT assigned before invocation');end;procedure TestParameterPassingMechanismOfFunctions;var
  X: IInterface;begin
  X := TInterfaceObject.Create;
  X := DoNothing; end;

演示呼叫TestParameterPassingMechanismOfFunctions()

您的代碼失敗是因?yàn)镈elphi和C+在函數(shù)結(jié)果傳遞機(jī)制方面對調(diào)用約定的理解不匹配。out參數(shù)。但對德爾菲來說var參數(shù)。

要解決這個問題,請嘗試如下:

function TestWideString: WideString; stdcall;begin
  Pointer(Result) := nil;
  Result := 'TestWideString';end;



查看完整回答
反對 回復(fù) 2019-11-06
  • 3 回答
  • 0 關(guān)注
  • 456 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號