This looks like C++/CLI.
1. Your gc objects need hats eg. "^".
2. you're passing pointer values which are not the same as references.
3. you fail to return the new pointer to the oracle interface.
I created a mock of the two MyDriver implementations to verify it works. I'm assuming a C++ class and not a ref class. I noticed the typo in the SQL server name.
#include "stdafx.h"
#include <stddef.h>
#include <string.h>
using namespace System;
struct MyDriver
{
int common;
public:
virtual const char *whoami() const = 0;
};
struct SQlClientDriver : MyDriver
{
int sql;
public:
const char *whoami() const { return "SQL"; }
};
struct OracleClientDriver : MyDriver
{
int oracle;
public:
const char *whoami() const { return "Oracle"; }
};
MyDriver* GetDSNAndProvider(String^ sName, String ^ &sDSN, String ^ &sProvider)
{
if(sName == "sqlServer")
{
sDSN="Data Source=(local);Initial Catalog=myDb;User Id=user;Password=pw;";
sProvider="System.Data.SqlClient";
return new SQlClientDriver(); }
else if (sName == "Oracle")
{
sDSN="Data Source=(local);Initial Catalog=myOracleDB;User Id=user;Password=pw;";
sProvider="System.Data.SqlClient";
return new OracleClientDriver(); }
return NULL;
}
int main(array<system::string xmlns:system="#unknown"> ^args)
{
Console::WriteLine(L"Starting ...");
String^ sDQN, ^sProvider;
MyDriver *MDriverIface = GetDSNAndProvider("sqlServer",sDQN,sProvider);
Console::WriteLine("Provider Name {0}", sProvider);
if (MDriverIface != NULL)
{
const char *whoami = MDriverIface->whoami();
IntPtr pass = (IntPtr)const_cast<char>(whoami);
String^ marshal = System::Runtime::InteropServices::Marshal::PtrToStringAnsi(pass, strlen(whoami));
Console::WriteLine(marshal);
}
Console::WriteLine(L"Done.");
Console::ReadLine();
return 0;
}
</char></system::string></string.h></stddef.h>
I purposely omitted the try/catch to make errors more obvious.