cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
anom217
Level 8

FileOpen failure

Basic MSI project here, and I'm trying to open a text file to edit, but for some reason the OpenFile function always fails when trying to open the file. I don't know why it's failing. My function is being called by another function in the InstallScript file, but the failure occurs here. Here is the code.


prototype openLogFile();

function openLogFile()
NUMBER nvFileHandle;
STRING dir, file;
begin
dir = "C:\\logs";
file = "test.txt";
OpenFileMode(FILE_MODE_APPEND);
if(ExistsDir(dir) < 0) then
CreateDir(dir);
endif;

if(Is(FILE_EXISTS, dir^file) < 0) then
MessageBox("File doesn't exist" ,0);
endif;
if(Is(FILE_LOCKED, dir^file) < 0) then
MessageBox("File is locked", 0);
endif;

if(OpenFile(nvFileHandle, dir, file) < 0) then
MessageBox("Failed to open " + dir^file, 0);
endif;
return nvFileHandle;
end;


The message pops up about failing to open the file, but I know it exists and isn't locked. OpenFile fails both when C:\logs\test.txt is already created and when it isn't. I tried changing the OpenFileMode to FILE_MODE_NORMAL and it still fails to open.

I'm not sure what's going wrong here, this seems like a fairly simple thing to do, yet it is strangely not working. Any help please?
Labels (1)
0 Kudos
(5) Replies
joshstechnij
Level 10 Flexeran
Level 10 Flexeran

Are you running this code from an immediate custom action? If so, you may not have permissions to create or open files in this location (C:\ tends to require admin privileges, so do most direct subfolders). In this case, the file either needs to be created in a location you have permissions to, the permissions on this folder need to be changed, or the action needs to run deferred in system context.
0 Kudos
anom217
Level 8

Thanks for the response. I am running it as a Deferred Custom Action. I have permission to write to files in this location. I've been able to get ConfigFileLoad and the associated configuration file functions to write to a file and work correctly. I'm not sure why OpenFile would fail? I feel like I'm at a brick wall here.
0 Kudos
joshstechnij
Level 10 Flexeran
Level 10 Flexeran

Since the OpenFile function does not return any system error codes on failure to open a file, there are two ways you can try to troubleshoot this behavior:
- Use the Process Monitor utility (from www.sysinternals.com) to monitor msiexec.exe and watch for access to the file being passed to OpenFile. Process Monitor will indicate the Win32 API status returned when attempting to open the file.

- Use the following code in your script at the point you normally call OpenFile:


nvResult = CreateFileA("C:\\logs\\test.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | 0x2, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(nvResult == INVALID_HANDLE_VALUE) then
SprintfBox(0, "CreateFile error", "Win32 CreateFile last error: %d", Err.LastDllError);
else
CloseHandle(nvResult);
endif;


The above CreateFile call is effectively the same call generated when calling the OpenFile function with the file mode set to FILE_MODE_APPEND. If the CreateFile call fails, the SprintfBox in the following if will display the Win32 error status set by CreateFile. This should provide some indication of why OpenFile is failing.
0 Kudos
anom217
Level 8

I used the CreateFile call, and it worked! At least, I didn't get any errors. So is nvResult the file handler? I tried passing it into WriteLine, and I didn't get any errors, but it's not writing to the file. The CreateFile call you gave me looks like it allows the file to be modified correct?

But this seems to be opening the file correctly. Thanks for your help!
0 Kudos
joshstechnij
Level 10 Flexeran
Level 10 Flexeran

The CreateFile call is calling the Win32 API directly instead of going through the InstallScript file functionality. The handle returned isn't interchangeable with other InstallScript file functions. The code I posted essentially is what the InstallScript OpenFile function will call internally.

At any other point in your script, are you opening or creating this test.txt file? Since the file could be opened directly by Windows, it is possible that some other checks done by InstallScript's OpenFile could be the cause of the failure.
0 Kudos