Unable to connect to Microsoft Access Database (C++) - c++

I've been going trough tons of articles and forums about this and I still have not found my solution. Even though there are several posts of this on this website aswell.
They state these things as answers:
Install this: http://www.microsoft.com/en-us/download/details.aspx?id=13255
I'm unable to install the x86 version, becouse I have office x64 2010. I did however installed the x64 version of that package.
Install this http://www.microsoft.com/en-us/download/details.aspx?id=23734
I've tried it, no results.
Make sure you application is targeted correctly (i.e. x86)
I'm 100% sure my application is 32 bit.
My work environment is as follows:
Visual Studio 2012 professional
Coding language is C++
Microsoft Office 2010
Windows 8 x64
This is the code that I'm using:
class Credential
{
public:
TCHAR CredentialID[3];
TCHAR CredentialName[255];
BEGIN_COLUMN_MAP(Credential)
COLUMN_ENTRY(1, CredentialID)
COLUMN_ENTRY(2, CredentialName)
END_COLUMN_MAP()
};
and this:
try
{
CDataSource ds;
CSession session;
ATL::CCommand<CAccessor<Credential>> cust;
HRESULT hr = CoInitialize(0);
if(FAILED(hr))
{
Console_Output("Can't start COM!?\n");
return;
}
hr = ds.OpenFromInitializationString(L"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MPFDatabase.accdb;");
Console_Output("%d\n", hr);
if(FAILED(hr))
{
Console_Output("Can't open Nwind\n");
return;
}
hr = session.Open(ds);
if(FAILED(hr))
{
Console_Output("Can't open Nwind SESSION\n");
ds.Close();
return;
}
TCHAR mySQL[] = "SELECT * FROM Credential";
hr = cust.Open(session, mySQL);
if(FAILED(hr))
{
Console_Output("Can't open Nwind TABLE\n");
session.Close();
ds.Close();
return;
}
while(cust.MoveNext() == S_OK)
{
Console_Output("%s -- %s\n", cust.CredentialID, cust.CredentialName);
}
cust.Close();
session.Close();
ds.Close();
}
catch(std::exception &Ex)
{
Console_Output("ex: %s\n", Ex.what());
}
I get to the point where it calls ds.OpenFromInitializationString but then it either stops my application without any notification whatsoever (no exception too). Or I get to the point where it would print in my console "Can't open Nwind\n".
These 2 results depend on what I've installed of the suggested answers.
I've tried absolute and relative paths for my Data Source.
I'm 100% sure it is not locked or something similar. I've created the database myself and it consists of only 2 tables, nothing special.
If I list my providers in windows powershell I get these providers:
SQLOLEDB
MSQLAP
MSQLAP
MSDataShape
SQLNCLI11
Microsoft.ACE.OLEDB.12.0
ADsDSOObject
SQLNCLI11 Enumerator
Windows Search Data Source
MSDASQL
MSDASQL Enumerator
SQLOLEDB Enumerator
MSDAOSP
So the ace engine is indeed installed as you can see.
EDIT:
Forgot to mention that it is concerning a local accdb file made in ms access 2010 on my computer. In case that wasn't clear.

As Gord has said. . .
How do you have a 32 bit application running on a 64 bit machine? First thing is to try to get those in sync.
I had a similar problem with access (but the application type was setup properly for me and I was in c#) so I will mention the three things that had an affect relating to the compatability problem as we had the same error message.
Goto the properties tab under Build. Then change Platform Target from the default Any CPU to x64 or x86. For me this was not optional it must be set to the right one when I deployed, but it worked fine under any CPU when I was debugging locally.
I also had to install the AccessDataBaseEngine_x64 which it sounds like you have already.
The next thing to check is your connection string.
For me my local was x86 and my server was x64.
For me both of these two worked with some playing around on this Jet and ACE settings:
string connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\your pathj\Database.accdb;";
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\your path\Database.accdb;Jet OLEDB:Database Password=password";
and I passed the connectionString instead of putting it into the field directly.
Hope this helps.

If you have the 64-bit version of the Access Database Engine -- a.k.a. "ACE" -- installed (as part of 64-bit Office 2010) and you are 100% sure that your application is running as 32-bit then I'm 100% sure that it won't work. 32-bit applications cannot use the 64-bit ACE driver, and 64-bit applications cannot use the 32-bit ACE driver. So, your choices are:
configure your application to run as 64-bit and use the existing 64-bit ACE driver, or
replace the 64-bit version of Office with the 32-bit version and run your application as 32-bit.

Related

NTFS signature is missing

I have loaded NTFS-3G from here: https://github.com/vitalif/ntfs-3g into a VS2017 project, wanting to read NTFS USB stick drive. And when I debugging reading process, I saw that is not reading USB drive due to:
// bootsect.c
BOOL ntfs_boot_sector_is_ntfs(NTFS_BOOT_SECTOR* b)
{
u32 i;
BOOL ret = FALSE;
ntfs_log_debug("Beginning bootsector check.\n");
ntfs_log_debug("Checking OEMid, NTFS signature.\n");
if (b->oem_id != const_cpu_to_le64(0x202020205346544eULL)) // "NTFS "
{
ntfs_log_error("NTFS signature is missing.\n"); // <-- my code is running here ...
goto not_ntfs;
}
...
...
I run it this code on Win10 64bit/VS2017. And my USB stick are valid, I have tried several USB stick (all of them NTFS).
I read that vitalif/ntfs-3g has a OS limitation: Windows XP, ... and Windows 7 file systems ... that is why I suspect the cause of my failing reading process ...
I have seek it on github for another NTFS-3G project that support Win10, I didn't found it ... is there anywhere another version of NTFS-3G library that work on Win10 ? Or, could another cause for my failing ?
Any support will be wellcome, I am struggle for this issue for days and days ...

Delete key from registry HKEY_LOCAL_MACHINE

I want to make uninstaller which should delete registry key from HKEY_LOCAL_MACHINE. Problem is that every time when i try delete location get LASTERROR 2, which is usually wrong name.
HKEY hKey = HKEY_LOCAL_MACHINE;
LSTATUS deletes = SHDeleteKey(hKey, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NameOfApp\\");
if (deletes == ERROR_SUCCESS)
{
return true;
}
I have same code in C# which work, but C++ not. Also try as administrator, still nothing (with C# didn't put any special privileges and work).
OS: Windows 7, 64 bit.
Do you have any idea, what is happening?
On 64-bit Windows there are two separated registry trees. One for 32-bit applications and the other for 64-bit ones. It depends on your application (32-bit vs 64-bit) which tree is used when calling SHDeleteKey and that could be why it is working with c# but not with c++.
You can use RegDeleteKeyEx to explicitly define the tree you want to access.

Detect emptiness of DVD-RAM media on Windows 7 x64

I'm trying to detect if a DVD-RAM media is empty or not, with C++ on Windows. The simplest choice is to use IMAPI (version 2) - boilerplate code omitted:
IMAPI_FORMAT2_DATA_MEDIA_STATE state;
HRESULT hr;
// ... Initialize an MsftDiscFormat2Data COM object and put recorder
hr = format->get_CurrentMediaStatus( &state );
// ... Verify returned status ...
return (state & IMAPI_FORMAT2_DATA_MEDIA_STATE_BLANK);
This code usually works perfectly. However, with DVD-RAM it gives the wrong results: the only flag enabled in the returned state is IMAPI_FORMAT2_DATA_MEDIA_STATE_OVERWRITE_ONLY ( = 0x1).
On Windows Vista 32 bit it works as expected.
Does anyone knows the reason for this result? Is there any workaround?
You can use the method IDiscFormat2::get_MediaHeuristicallyBlank from IDiscFormat2 interface.
It will attempt to determine if the media is blank using heuristics (mainly for DVD+RW and DVD-RAM media).
VARIANT_BOOL vbBlank;
hr = format->get_MediaHeuristicallyBlank(&vbBlank);
if (VARIANT_TRUE == vbBlank)
Log("The media is blank.");
In order to determine if the current media is reported as physically blank by the drive you can use IDiscFormat2::get_MediaPhysicallyBlank method.
As for the reasons of the different behavior between Windows7 x64 and Windows Vista x86, it could be because IMAPIv2 versions may be different on those systems. You may want to update your Vista machine with the latest Image Mastering API v2.0 update package to get the same results on each system.

Why does my code fail to create a directory in "C:\Program Files" under Windows 7?

I am using Windows 7 and I have to run one program in that windows but that program working in Windows XP. This is a Visual C++ program and I am using Visual Studio 2008 for this. When I am running my application, it does not throw any errors, but it does not create a directory in "c:\program files\". So can anyone help me to create directory and exe file?
This is the code I am using:
char szAppPath[MAX_PATH];
char szFileName[MAX_PATH];
DWORD dwResult;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
dwResult = ExpandEnvironmentStrings( NULL, szAppPath, MAX_PATH); // "%ProgramFiles%"
// do same for NSim directory
strcat(szAppPath,"\\NSim");
hFind = FindFirstFile(szAppPath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
//Directory Does't Exists create New
if(!CreateDirectory(szAppPath,NULL)) //Throw Error
{
MessageBox("Unable to Create N-SIM directory","NSim Installer");
return ;
}
}
else
{
//check if is directory or not
if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
MessageBox("Can't Create N-SIM directory\n Another file with same name exists","NSim Installer");
return ;
}
FindClose(hFind);
}
//***************************************N-SIM Application****************************
strcpy(szFileName, szAppPath);
HRSRC hRes;
if( bRegister == FALSE)
{
strcat(szFileName,"\\NSim.exe"); //make same name of the Client & Server in program file
hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_LANSIMSERVER),RT_RCDATA);
if(flagUpgrade ==0)
{
CString trial = installationDate(); //----- Detemine Expiry Date -----
setRegistry(trial);
}
}
It's a file permissions issue, plain and simple. Programs can't just go rooting around system directories in Windows 7. That's why it works "properly" in Windows XP, but not in newer versions.
I can't tell for sure, but it looks like you're trying to write an installer. If so, why are you reinventing the wheel? There are tons of great setup utilities availableā€”Visual Studio provides a setup project that you can customize to your needs, or look into Inno Setup, my personal favorite. A Google search will turn up plenty of other options that have already solved this problem for you, and innumerable others.
If this isn't an installer, and you're just trying to store application and/or user data in the Program Files folder, I highly recommend that you look elsewhere. You weren't supposed to shove data into the app folder under earlier versions of Windows, and Windows 7 just cuts you off at the knees if you do this. Your best bet is to follow the recommendations that existed from the beginning: Investigate the user and common Application Data folders carefully. Use the SHGetKnownFolderPath function to retrieve the full path to a known folder using its KNOWNFOLDERID. A couple of suggestions:
FOLDERID_ProgramData (a shared program data directory for all users)
FOLDERID_LocalAppData (a per-user program data directory, non-roaming)
FOLDERID_RoamingAppData (a per-user program data directory, roaming)
Alternatively, you can try running the application as an Administrator. You might want to look into creating a manifest that indicates the application requires administrator-level permissions to execute.
[edit] I edited the code in the question for readability and removed the commented out code (to see the wood for the trees). It is now obvious that nothing initialises szAppPath before calling strcat(), and calling ExpandEnvironmentStrings with NULL as the first argument is undefined (and certainly useless). Calling strcat() on an unitialised string is not likely to have the desired result. This may be an artefact of not posting the real code, or even of other peoples edits (including mine).
CreateDirectory sets the system error code on error; if you want to know what went wrong, check it! Any answer you get here will be an educated guess.
if(!CreateDirectory(szAppPath,NULL)) //Throw Error
{
DWORD errorcode = GetLastError();
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL );
MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK);
return ;
}
If you just want to get the error code and look it up manually, then a complete directory of codes is available on MSDN, here, I would guess that ERROR_ACCESS_DENIED
(5) is most probable. A more elaborate example of error code display is given here.
windows7?
Ok, the problem is not with your program. Its with the file system permissions in Windows 7. User programs cannot create files there.
I think the problem is lack of privileges. You can debug your project to see whether the CreateDirectory function sets an error as ERROR_ACCESS_DENIED, if it does, you should make your program run with an administrator privilege. Add manifest in your project to do so.
It is intended to protect your computer against attack. Well maybe. Or Microsoft deciding to tell you what you are and not allowed to do on your own computer.
In any case you can change your UAC settings if you really have to write there in that way although that obviously exposes you to risk.
Otherwise play nice and do things the Microsoft way, using a proper installer.

How to allow 32 bit apps on 64 bit windows to execute 64 bit apps provided in Windows\System32

Say you have an app, that you want to provide users ability to browse the system32 directory and execute programs in (like telnet).
What is the best method for supporting this when you need to support XP onwards as a client and 2k onwards for server?
Having written all this up I wonder if it's just too much time/effort in providing a browse to do this, where they could just copy it from explorer. Still requires ability to launch.
I have found some discussion on Nynaeve.
So far it seems there are the following options
Create a sysnative folder in windows which will allow you to browse/execute 64 bit. Issues are:
only available in Vista/Longhorn, so no support for XP 64
leads to different path naming, can't use same path on multiple versions.
will be active for whole of windows, not just our app
may not (probably is not) appropriate to do when installing the app
allows to specify explicitly through path only which version of the app to launch if there is a 32 bit and 64 bit version
Use the windows API to temporarily disable the redirection when showing file lists or executing users run commands. Issues are:
Only available on 64 bit - have to mess with GetProcAddress
available only under certain service packs
must individually identify all locations that this should be implemented
user will need to provide seperate information about whether this is a 64 bit app or 32 bit.
If anybody had some example code which displayed a Windows OpenFile dialog (say using MFC CFileDialog) showing nativly for XP/Vista and allowing the viewing of 64 bit system32 directory, that would be awesome.
If anybody had an example of launching the named app, that would also be great!
Edit:
Currently we use CreateProcess for launching the app (which is failing).
err = CreateProcess((wchar_t*)exeName.c_str(), (wchar_t*)cmdLine.c_str(), NULL, NULL, FALSE, CREATE_SEPARATE_WOW_VDM, NULL, workingDir.c_str(), &startupInfo, &processInfo);
I've gone with option 2,
For those who might be interested; here is my quick hack at a scoped version of managing the disabling of Wow64 redirection based on notes from MS. Will redirect if the API is available, expects that kernel32.dll is already available.
class Wow64RedirectOff {
typedef BOOL (WINAPI *FN_Wow64DisableWow64FsRedirection) ( __out PVOID *OldValue );
typedef BOOL (WINAPI *FN_Wow64RevertWow64FsRedirection) ( __in PVOID OldValue );
public:
Wow64RedirectOff() {
LPFN_Disable = (FN_Wow64DisableWow64FsRedirection)GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"Wow64DisableWow64FsRedirection");
if( LPFN_Disable ) {
LPFN_Disable(&OldValue);
}
}
~Wow64RedirectOff() {
if( LPFN_Disable ) {
FN_Wow64RevertWow64FsRedirection LPFN_Revert = (FN_Wow64RevertWow64FsRedirection)GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"Wow64RevertWow64FsRedirection");
if( LPFN_Revert ) {
LPFN_Revert(OldValue);
}
}
}
private:
FN_Wow64DisableWow64FsRedirection LPFN_Disable;
PVOID OldValue;
};
And thus usage would be
Wow64RedirectOff scopedRedirect;
//CFileOpen
//CreateProcess
Are you getting redirected to Windows/SysWoW64? I can launch 64-bit apps from Windows/System32 from an OpenFile in a 32-bit managed executable.