NI產品與技術 討論區

取消
顯示結果 
搜尋替代 
您的意思是: 

詢問Labview call library function 呼叫 Delphi dll 問題

已解決!
前往解決方案

Dear

在使用labview call library function to delphi dll 的問題,如下圖所示:

frontPanl.PNGblock dilgram.PNGlibrary parameter.PNG

 

下圖為Delphi function parameters

 

delphi fCanIGo fucntion paramater.PNG

 

這樣call dll的方式是對的嗎?對於Delphi的pch對應到Labivew 的參數定義是單純的String?

 

 

 

 

0 積分
1 條訊息(共 7 條)
3,944 檢視

您好,
很抱歉,基於對Delphi并不是很理解,這方面的資料也很少,我只能協助提供您一些方向作參考
關於1097的錯誤資訊可以參考以下的連結
https://forums.ni.com/t5/LabVIEW/Error-Code-1097-Coming-in-DLL-Calling/m-p/1847055
https://forums.ni.com/t5/LabVIEW/How-to-pass-data-from-a-Delphi-pascal-DLL-class-to-a-LabView/td-p/8...
https://forums.ni.com/t5/forums/forumtopicpage/board-id/170/message-id/958808/agent-type/internal-cr...

它們宣告的資料形態都有對應嗎?如果一方是宣告integer,另一方是string就可能出現錯誤。
也可能需要將對應的dll添加到LabVIEW.exe所在的文件夾中,以避免發生錯誤。

0 積分
2 條訊息(共 7 條)
3,910 檢視

Hi AlysaOng

首先感謝您的提供的資料,目前有先將dll 檔與labview.exe,但執行過後還是一樣有同樣的error code,

不過手頭上有透過C# Call 此dll檔運用的Code ,因為不是很懂C#能請你幫忙解析一下用法差別在哪嗎?

 

C# Import DLL Code

 

1.[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary(string DllName);

 

2.[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true)]
private delegate int fpCanIGo(string szServerIPAddr, string szProdModel, string szTrackSerial, string szTestStation, string szOperatorID, string szMachName, StringBuilder szSFCResponse);

 

3.l_pDll = LoadLibrary(strASFCSTPDLL1Path);

 

4.pCanIGo = GetProcAddress(l_pDll, "fCanIGoTest");

 

canGoTest = (fpCanIGo)Marshal.GetDelegateForFunctionPointer(pCanIGo, typeof(fpCanIGo));

 

5.StringBuilder respMsg = new StringBuilder(1000);
int canGoTestOK = 0;
lock (lockGoNoGo)
canGoTestOK = canGoTest(
myServerIPAddr, // SFC server IP Address
myProdModel, // Product Model Name
AXV_TrackSN, // Test Unit Tracking Serial Number
myTestName, // Test Name
myOperatorID,
myMachName,
respMsg);
AXV_SFCGoCheckResponse = respMsg.ToString().Trim();
respMsg.Clear();
AXV_SFCExeGoChkCode = canGoTestOK;
retBool = (canGoTestOK == 0);

 

我把程式一共分成5個部分解析

 

1.對於第2部分C#在要load dll 時做了一些宣告,UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true),在這個函式中CallingConvention 適用StdCall 所以符合了Labview Call library function 頁中的calling convention 的方式,CharSet =CharSet.Ansi,這個主要是在宣告未來運用String 的資料形式嗎?針對Ansi意思:封送處理字串為多位元組字元字串,不太了解

 

2.宣告Function fpCanIGo時,後面帶著相關參數,String szServerIPAddr ,在C#裡面這個String 跟Labivew 中的String 會有差異嗎? 我看C#中的String 是UTF-16,Labview 中的Sting 也同樣是一樣的型態嗎?

 

3.在Call library function parameter頁面中對於string format的型態區別在哪裡,有看help 裡面的解釋,但有點不懂,如下圖所示:

 

string formate.PNG

 

以上,非常感激.

0 積分
3 條訊息(共 7 條)
3,907 檢視

1. 是的,CharSet =CharSet.Ansi 表示編碼方式,用於將C#的Unicode UTF-16 string編組為ANSI。
或比如在呼叫labVIEW的.dll,把有關内容都指定為Ansi資料形式。

2. Labview 中的string是ASCII,但可以把它轉爲UTF-16
https://forums.ni.com/t5/LabVIEW-Idea-Exchange/Make-the-string-type-contain-subgroups-for-ASCII-and-...
https://forums.ni.com/t5/LabVIEW/Unicode-in-LabVIEW/td-p/3634872

3.以下三種都是指向string的pointer,它們的差異為:
C String Pointer 大多數Win32 API函數都使用此字符串pointer。string中允許使用NULL字符。字符串是
幾乎無限長度(最多231個字符)。
Pascal String Pointer 前面是一個length byte。它的長度限制為255個字符。
LabVIEW String Handle 與String handle pointer相似,可視爲是String handle Pointer的指標。
LabVIEW String Handle Pointer 前面是四個bytes的長度信息,例用來記錄後方的内容總共有多少bytes。

這裏有更詳細的解説:
https://m.eet.com/media/1089230/an087.pdf
http://zone.ni.com/reference/zhs-XX/help/371361R-0118/lvexcodeconcepts/array_and_string_options/

0 積分
4 條訊息(共 7 條)
3,895 檢視

Hi AlysaOng

真的很感謝你這麼專業的解答,照著您的指導我有著手在修正Call library Function 目前的的用法,因為真的太多疑問想跟您這邊求解,真的很不好意思,還有一些疑問想在請教您,

1.關於上續的第3點對於C#的字串模式,labview 的String Format :C String Pointer 會是相容的嗎?

 

2.其實目前還有一個非常大的疑問,C#中有一個StringBulider 這個函數,對於Labview 語言來說會有這個相同的用法嗎?感覺像是Var

 

3.然後我看大家用了這個StringBulider 後,如果要解這個位置裡面的內容就必須在用一個ToString(),我看網路上Labivew 好像都是運用在.net如果對應在Call library function 也有可以用ToString 的用法?

 

以上,真的很感激您這麼信心的指導.

0 積分
5 條訊息(共 7 條)
3,892 檢視
解決方案
接受者 scottchen

Hi Scottchen

我很樂意回答您的問題,也謝謝您給予的鼓勵,雖然本身也不太懂C#,但會盡最大所能,希望能幫到您。

1.抱歉,這問題無法給您確定的答案。以下的論壇有提到說C#的string是個物件,而非C string pointer。
https://forums.ni.com/t5/LabVIEW/Problem-in-calling-LabVIEW-DLL-with-C/td-p/2627315

2.如果是要使用StringBuilder裏的内容,可以參閲這方法
https://forums.ni.com/t5/LabVIEW/how-to-get-string-from-a-stringBuider-reference/td-p/1411316

3.關於這問題或許可參考這一篇
Converting data from a generic LabVIEW object to a string like the Object.ToString method in .NET
https://forums.ni.com/t5/LabVIEW/converting-data-from-a-generic-LabVIEW-object-to-a-string-like/td-p...

其他參考資料:
Function Parameters for LabVIEW DLL
https://stackoverflow.com/questions/19912212/function-parameters-for-labview-dll?rq=1
ToString Method
http://zone.ni.com/reference/en-XX/help/370627F-01/mstudiowebhelp/html/faecf8ce/
https://forums.ni.com/t5/LabVIEW/pass-stringbuilder-argument-to-dll/td-p/3834965

0 積分
6 條訊息(共 7 條)
3,860 檢視

Hi AlysaOng

感謝您提供的資料,目前此問題已經找到方法解決,

 

以上,感激不盡.

0 積分
7 條訊息(共 7 條)
3,839 檢視