Visual Studio 2012 : Oracle Database program using VC++ (MFC) & Oracle OCCI Library - c++

I tried to connect the oracle XE (11.2.0.2.0) database using VC++ MFC (Visual Studio 2012). Below is a example code given at oracle website.
void CMFCOracleOCCIDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
const string userName = "<username>";
const string password = "<password>";
const string connectString = "";
Environment *env = Environment::createEnvironment();
{
Connection *conn = env->createConnection(userName, password, connectString);
Statement *stmt = conn->createStatement("SELECT * FROM tab");
ResultSet *rs = stmt->executeQuery();
for(int i=1;i<20;i++){
rs->next();
//Below are the commented 4 lines
/*
string s = rs->getString(1);
CString str2(s.c_str());
lstBox01.AddString(str2);
MessageBox(str2);
*/
}
stmt->closeResultSet(rs);
conn->terminateStatement(stmt);
env->terminateConnection(conn);
}
Environment::terminateEnvironment(env);
}
In the above code while the 4 line in comment, it works fine. Otherwise it works for only one loop (i=1) and display the proper message (for only 1 time). After that, getting the below error
First-chance exception at 0x51A4CCC8 (msvcp110d.dll). After selecting the break it goes to the file xutility and function
inline void _Container_base12::_Orphan_all()
How to solve this error?
Usually what is best practice to connect oracle using VC++ MFC?
Environment :
Windows 8.1 (64)
Oracle XE (11.2.0.2.0)
OCCI Library : Instant Client Package - SDK: Additional header files and an example makefile for developing Oracle applications with Instant Client
Download instantclient-sdk-nt-11.2.0.2.0.zip
Visual Studio 2012 (Debug - Configuration)
Edit
This error raised in-case of getString() (varchar2 field). When using integer field with getInt(), it is working fine.

Related

SAP Netweaver. C++ Error:The memory cannot be read

For my project, I need to test if the router string connects with SAP server successfully using SAP Netweaver 7.0. I have created a Console application in Visual studio 2017. I have given the connection parameters. But the application ends with an error: The memory cannot be read
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"
#include "include/sapnwrfc.h"
void errorHandling(RFC_RC rc, RFC_ERROR_INFO*errorInfo, RFC_CONNECTION_HANDLE connection) {
printfU(cU("%s: %s\n"), errorInfo->key, errorInfo->message);
/* It's better to close the TCP/IP connection cleanly, than tojust let the backend get a "Connection reset by peer" error...*/
if (connection != NULL) RfcCloseConnection(connection, errorInfo);
exit(1);
}
int mainU(int argc, SAP_UC** argv)
{
RFC_RC rc = RFC_OK;
RFC_CONNECTION_PARAMETER loginParams[6];
RFC_ERROR_INFO errorInfo;
RFC_CONNECTION_HANDLE connection;
// -----------------------------------------------
// OPEN CONNECTION
// -----------------------------------------------
// Create logon parameter list
loginParams[0].name = cU("gwhost");
loginParams[0].value = cU("80.**.***.**");
loginParams[1].name = cU("sysnr");
loginParams[1].value = cU("00");
loginParams[2].name = cU("client");
loginParams[2].value = cU("800");
loginParams[3].name = cU("user");
loginParams[3].value = cU("jsar12");
loginParams[4].name = cU("lang");
loginParams[4].value = cU("EN");
loginParams[5].name = cU("****");
loginParams[5].value = cU("Abc");
// Open connection
connection = RfcOpenConnection(loginParams, 6, &errorInfo);
if (connection == NULL) errorHandling(rc, &errorInfo, NULL);
return 0;
}
Expected Result: Connection to the SAP Router
The solution for the above problem was in the settings of the Visual Studio. After a lot of searching and trying out a few tricks I have found the solution in the following link:
https://answers.sap.com/questions/12367200/netweaver-rfc-connector-error-logon.html
Summary:
On Linux and Windows, certain minimum releases of the C runtime are required to execute the programs. See SAP Notes 1021236 (for Linux) and 684106 (for Windows). The compiler and linker options needed to compile programs using the SAP NetWeaver RFC SDK are listed in SAP Note 1056696.
In addition, for Windows users using Microsoft Visual Studio, this is a description of how to set the Visual Studio project properties:
General section: Make sure the CharacterSet field is set to “Use Unicode Character Set.”
Debugging section: Under Environment, add something like Path=%Path%;nwrfcsdk\lib.
C/C++ section:
General: Add the nwrfcsdk\include directory under Additional Include Directories.
Preprocessor: Add the two preprocessor definitions SAPonNT and SAPwithUNICODE.
Code Generation: Choose “Multi-threaded DLL (/MD)” as the Runtime Library; selecting “Multi-threaded Debug DLL (/MDd)” may lead to strange problems.
Linker section:
General: Add the nwrfcsdk\lib directory under Additional Library Directories.
Input: Under Additional Dependencies, add libsapucum.lib, sapnwrfc.lib, and sapdecfICUlib.lib

ConfigurationManager issue in VS 2017

The following code, when run in Visual Studio 2015 and earlier, results in a message box being shown with the expected value "12345".
string executablePath = Application.ExecutablePath;
executablePath = Path.GetFileNameWithoutExtension(executablePath);
executablePath = executablePath + ".vshost.exe";
if (!File.Exists(executablePath))
throw new FileNotFoundException(executablePath);
Configuration cfg = ConfigurationManager.OpenExeConfiguration(executablePath);
cfg.AppSettings.Settings.Add("Testing", "12345");
cfg.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(cfg.AppSettings.SectionInformation.Name);
string testing = ConfigurationManager.AppSettings["Testing"];
MessageBox.Show(testing);
When I run the same code in Visual Studio 2017 the message box displays a blank value.
Is this a bug in Visual Studio 2017 or does the code require modification?
UPDATE (specific cause):
So the main cause, along with the accepted answer, was that I had opened the solution in VS 2015 which generated the *.vshost.exe related files. Later I opened the solution in VS 2017 and, of course, the *.vshost.exe files aren't cleaned automatically so were still there.
UPDATE 2 (for those who want to be able to use similar code in both):
string executablePath = Application.ExecutablePath;
executablePath = Path.GetFileNameWithoutExtension(executablePath);
executablePath = executablePath + ".vshost.exe";
// Check if the *.vshost.exe exists
if (File.Exists(executablePath))
{
try
{
// If deleting throws an exception then the stub is being run by *.vshost.exe while
// debugging which means this is NOT Visual Studio 2017 (*.vshost.exe is no longer used in VS 2017)
File.Delete(executablePath);
// If it deletes then use the regular app path since VS2017 is using that now.
executablePath = Application.ExecutablePath;
}
catch (Exception)
{
executablePath = Application.ExecutablePath;
}
}
else
executablePath = Application.ExecutablePath;
Configuration cfg = ConfigurationManager.OpenExeConfiguration(executablePath);
cfg.AppSettings.Settings.Add("Testing", "12345");
cfg.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(cfg.AppSettings.SectionInformation.Name);
string testing = ConfigurationManager.AppSettings["Testing"];
MessageBox.Show(testing);
The debugger hosting process has been removed in VS2017, so when you run your application the path you give to the OpenExeConfiguration method is incorrect.
Instead, pass Application.ExecutablePath to that method and it should work. The same should work in VS 2015 if you turn off the hosting process (Project properties -> Debug -> Enable the Visual Studio hosting process).
It's odd that you were using the hosting process path in the first place, as that would only ever work while running in the debugger from within Visual Studio.

OCCI app crashes when running in debug mode in Visual Studio 2005

I'm attempting to get a development environment up and running for developing applications with Oracle C++ Call Interface (OCCI) in Visual Studio 2005.
My system specs are:
OS: Windows 7, 64-bit
Oracle: 11g release 11.2.0.2, 32-bit
Instant Client: BasicLite and SDK version 11.2.0.4 32-bit
Visual Studio 2005 Professional Edition version 8.0 with 32-bit tools enabled
I've followed this guide by Mark Williams and I got the example running but only in release mode. When I switch to debug mode the app will build, but when I run it I get the following error:
Problem signature:
Problem Event Name: APPCRASH
Application Name: OCCITest.exe
Application Version: 0.0.0.0
Application Timestamp: 53f5dfdd
Fault Module Name: KERNELBASE.dll
Fault Module Version: 6.1.7601.18229
The small example program that triggers this error is:
#include "employees.h"
using namespace std;
using namespace oracle::occi;
int main (void)
{
Employees *pEmployees = new Employees();
delete pEmployees;
return 0;
}
Employees::Employees()
{
user = "hr";
passwd = "hr";
db = "localhost:1521/service_name";
env = Environment::createEnvironment(Environment::DEFAULT);
try
{
con = env->createConnection(user, passwd, db);
}
catch (SQLException& ex)
{
cout << ex.getMessage();
exit(EXIT_FAILURE);
}
}
Employees::~Employees()
{
env->terminateConnection (con);
Environment::terminateEnvironment (env);
}
If I remove all calls to OCCI functionality the application doesn’t crash. That is, this program runs error-free:
#include "employees.h"
using namespace std;
using namespace oracle::occi;
int main (void)
{
Employees *pEmployees = new Employees();
delete pEmployees;
return 0;
}
Employees::Employees()
{
user = "hr";
passwd = "hr";
db = "localhost:1521/service_name";
cout<<"Look at me, I'm running"<<endl;
}
Employees::~Employees()
{}
In the guide Mark mentions that when running in debug mode, the linker should use the library file oraocci11d.lib. However, this file is not included in the Instant Client SDK version 11.2.0.4, so I’m using the input file oraocci11.lib for both the release and debug version.
I'm running out of ideas about how to proceed in solving this problem, and I would greatly appreciate any and all help.
If the Oracle DLL receives and/or passes objects such as std::string or any other object that either:
Manipulates the heap in any way, or
The objects could have differing internals between app and DLL,
then you have no choice but to use the correct library to link with. Otherwise you wind up with binary or heap incompatible objects being passed, which leads to what you're seeing now.
See here: http://docs.oracle.com/cd/E11882_01/appdev.112/e10764/install.htm#CBHGBBJI
The link above mentions both the debug import library and debug version of the DLL. Also this is stated at the link:
Applications that link to MSVCRTD.DLL, a debug version of Microsoft C-Runtime, /MDd compiler flag, should link with these specific OCCI libraries: oraocci11d.lib and oraocci11d.dll.
Since it took me quite some time to get the debug environment working I figured I'd answer my own question now that I did.
I got a variety of errors throughout the ordeal, but the error that I got most stuck on was an error saying:
'The application was unable to start correctly (0xc0150002).
Click OK to close the application.'
Also, I used http://www.dependencywalker.com which repeatedly told me that either oraocci11d.dll or a the following list of dll's could not be found.
API-MS-WIN-APPMODEL-RUNTIME-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-ERROR-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-ROBUFFER-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL
API-MS-WIN-SHCORE-SCALING-L1-1-1.DLL
DCOMP.DLL
IESHIMS.DLL
However, what was really missing was for the executable to be able to find oci.dll. I'm just mentioning the errors in case someone else runs into these.
Here is what was needed to make it work:
First of all, the Instant Client does not contain the oraocci11d.lib or oraocci11d.dll, so it is necessary to install the full Oracle Client.
Next, the following must be added to the PATH:
C:\Program Files\Oracle\11.2.0\OCI\lib\MSVC\vc8
C:\Program Files\Oracle\11.2.0\BIN
In Visual Studio, select Tools -> Options, unfold 'Projects and Solutions' and select VC++ Directories. In 'Show directories for' under:
Include Files add C:\Program Files\Oracle\11.2.0\OCI\include
Library files add C:\Program Files\Oracle\11.2.0\OCI\lib\MSVC\vc8
In the property page for your project under Configuration Properties -> Linker select Input and under Additional Dependencies add oraocci11d.lib (or oraocci11.lib for release mode). Then select debug/release mode in the Configuration Manager
I have a related problem in that I am successfully using oraocci12d.dll/msvcr100d.dll, but this in turn is using oci.dll/msvcr100.dll. ie, oci.dll is not using the debug version of msvcr100.
My program seems to run okay, but any memory leak reporting disappears on exit.

how to create FoxPro dbf files from c++?

I need to read a FoxPro Dbf File and recreate same in c++; I installed foxpro driver version 9.0 but it did not appeared in driver list in windows DSN. I then installed foxpro from visual studio 6 setup and that showed the driver there and I was able to make connection using it through ADO .Problem is that
_ConnectionPtr m_pCon;
_RecordsetPtr m_pRecordset;
AfxOleInit();
_bstr_t bt1(PassedCstringDirectory);//PassedCstringDirectory passed as argument
_bstr_t bt(sqlcmd);//sqlcmd passed as argument
m_pCon.CreateInstance(__uuidof(Connection));
HRESULT hr;
VARIANT *rowseffected=NULL;
try
{
//The SourceDB should be a proper directory
hr=m_pCon->Open("Provider=VFPOLEDB; SourceType=DBF; "
"SourceDB="+bt1+";Extended Properties=FoxPro 3.0;", "","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox("Connection failed, check the direction!");
}
m_pCon->Execute(bt,rowseffected,adConnectUnspecified);
m_pCon->Execute() is returning false(S_FALSE) but files (dbf and fpt) are being created .and i am not able to insert data into files .error being ."Current Recordset does not support updating. This may be a limitation of the provider, or of the selected locktype."

Visual Studio 2013 crashes when compiling Sqlite C++ library

I'm having an absolute nightmare of a time trying to get the Sqlite C++ library to compile in Visual Studio 2013 Ultimate (Compiles fine in VS2012).
Basically regardless of whether I try to perform a clean or a rebuild VS will claim to successfully finish but will subsequently freeze and become unresponsive, never to recover.
Here is the output
and here is the actual VS project.
Anyone care to give it a crack and see if they are running into the same problem or provide any suggestions?
Tim Heuer gives step by step instructions ON THIS LINK. The batch files are hardcoded for TCL 8.5, you'll save yourself some time if you don't download the latest (8.6)
EDITED - I successfully compiled SQLite with Tim's steps (I just reinstalled Windows 8.1 / VS 2013). Note: the only issue I encountered was taking the steps to literally, be sure to change into the newly created SQLite directory before running the fossil command.
FYI for WinRT, be sure to use the correct path, if you just specify the filename you will get an access denied error (which will surface as a "cannot open database" error).
using namespace Windows::Storage;
using namespace std;
void SqliteWrapper::RunTest(void)
{
sqlite3 *db;
int rc;
auto path = ApplicationData::Current->LocalFolder->Path+"\\MyDatabase.db";
string dataPath(path->Begin(), path->End());
rc = sqlite3_open(dataPath.c_str(), &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
}
sqlite3_close(db);
}