Hemang Maradiya
Posts: 4
Joined: 2008-02-18
|
We have developed Excel Add-in application with Add-in Express 2005 Standard Edition and c# 2005.
While reading adxloader.txt log file through seperate console application and at that point of time, Excel is already opened then it gives file sharing violation exception and my console application is not able to read adxloader.txt log file contents.
Once Excel and ADXloader were launched then after also my application is not able to read file.
My application is using .net FileStream object with attributes FileAccess.Read and FileShare.Read to read log file. Eventhough my application is not able to read log file and it give "Sharing violation Exception".
Can you please guide, how to overcome from file sharing violation or read log file through programatically while Exel is arleady launched?
Thanks in advance
Hemang
|
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Hemang.
What do you need to read the adxloader.log file for? |
|
Hemang Maradiya
Posts: 4
Joined: 2008-02-18
|
Hi Sergery,
We have developed some diaognostic utility application to check prerequisite, user rights and logs. With this application we want to read adxloader.log file also.
|
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Hemang.
In this case you need to use the mutex and open the adxloader.log file in the shared mode. Here is the loader code:
const LPTSTR MUTEX_NAME = L"ADXLoggerMutex";
const LPTSTR LOG_FILENAME = L"adxloader.log";
CDebugPrintf debug;
CDebugPrintf::CDebugPrintf ()
{
USES_CONVERSION;
DWORD x;
TCHAR szDirectory[MAX_PATH + 1] = {0};
TCHAR szTempDirectory[MAX_PATH + 1] = {0};
TCHAR szFileFullName[MAX_PATH + 1] = {0};
TCHAR szLogFileDirectory[MAX_PATH + 1] = {0};
try
{
m_hFile = NULL;
logEnabled = false;
shadowCopyEnabled = true;
adminRegistration = true;
m_hDebugMutex = NULL;
if (::GetDllDirectory(szDirectory, sizeof(szDirectory)/sizeof(szDirectory[0])) == S_OK)
{
lstrcat(szFileFullName, szDirectory);
...............
szFileFullName[0] = '\0';
if (lstrlen(szTempDirectory) == 0)
{
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL | CSIDL_FLAG_CREATE, NULL, 0, szTempDirectory)))
{
lstrcat(szTempDirectory, L"\\Add-in Express\\");
}
}
if (lstrlen(szTempDirectory) != 0)
{
lstrcat(szFileFullName, szTempDirectory);
lstrcat(szFileFullName, LOG_FILENAME);
if (/*!loadOkay ||*/ logEnabled)
{
m_hDebugMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, MUTEX_NAME);
if (m_hDebugMutex == 0)
{
char buffer[1024] = {0};
if (loadOkay)
m_hDebugMutex = CreateMutex( 0, FALSE, MUTEX_NAME);
CreateDirectory(szTempDirectory, NULL);
HANDLE hFile = CreateFile(szFileFullName, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
WriteFile(hFile, HEADER, (DWORD)strlen(HEADER), &x, 0);
SYSTEMTIME time;
GetLocalTime(&time);
sprintf(buffer, "%02d/%02d/%02d %02d:%02d:%02d",
time.wMonth, time.wDay, time.wYear, time.wHour, time.wMinute, time.wSecond);
WriteFile(hFile, buffer, (DWORD)strlen(buffer), &x, 0);
WriteFile(hFile, CrLf, (DWORD)strlen(CrLf), &x, 0);
/*if (!loadOkay)
{
buffer[0] = '\0';
sprintf(buffer, "Error loading the manifest file. The 'adxloader.dll.manifest' file has a wrong format or it doesn't exist in the '%s' directory.", T2A(szDirectory));
WriteFile(hFile, buffer, (DWORD)strlen(buffer), &x, 0);
}*/
CloseHandle(hFile);
}
}
if (loadOkay && logEnabled)
{
CreateDirectory(szTempDirectory, NULL);
m_hFile = CreateFile(szFileFullName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
SetFilePointer(m_hFile, 0, 0, FILE_END);
WriteFile(m_hFile, CrLf, (DWORD)strlen(CrLf), &x, 0 );
WriteFile(m_hFile, "Startup directory: ", 19, &x, 0);
WriteFile(m_hFile, "Loader version: 4.0.1902 ", 25, &x, 0);
WriteFile(m_hFile, T2A(szDirectory), lstrlen(szDirectory), &x, 0);
WriteFile(m_hFile, DIVIDER, sizeof(DIVIDER), &x, 0);
InitializeCriticalSection( &m_hLock );
}
}
}
}
catch(...)
{
}
}
|
|