LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

dll cannot find other .ini file

Solved!
Go to solution

hello everyone

I use labview2020 , I build a testfun.dll with visual studio ,and the dll will read  the nanodllparam.ini in the same folder ,when I use my testmain.vi to call dll,a problem occurs , when I doubleclick the testmain.vi,the vi call the dll and dll can find the ini file in the current filefolder, but when I start the labview with the icon labview.exe on the desktop, and from the file-open the testmain.vi and dll cannot find the ini file,I test the currentfolder with c++ ,maybe the doubleclick return the path is different from the icon starts, i wish the dll can find the ini path in the vi folder when I start the labview.exe icon , but I have to reserve the ini file ,and how can i resolve the problem?  thanks very much

 

0 Kudos
Message 1 of 3
(1,306 Views)
Solution
Accepted by topic author love8668love

That’s a Windows feature. When you use a relative path to access files the Windows system will automatically prepend the processes Current Directory to the path. The Current Path is a process global variable that is initialized on process startup to the same directory where the exe file is located. It can be explicitly changed by calling the SetCurrentDirectory() Windows API. In addition to that will the file selection dialog also update it to the directory in which you selected the last file. Consequence: it’s pretty much useless.

Do NOT change this directory with the Windows API prior to calling your DLL function. While this would usually work there is a serious potential for race conditions as there is absolutely no warranty that this global variable won’t change between you setting it and your DLL trying to access the file through a relative path. 

The right fix is to calculate the right absolute path in your DLL. You can do that by calling the GetModuleHandle() API with the name of your DLL and then pass that handle to GetModuleFileName(). Remove the file name of the DLL and append instead the filename of your ini file.

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 3
(1,273 Views)

thanks for your response,rolfk .I modify the code as your method,and it works either way,the dll can find the ini file. Thanks a lot.

0 Kudos
Message 3 of 3
(1,224 Views)