Celetron
asked on
How to use a c#.net dll in vc++6 programmes?
I am trying to figure out if it is possible to use a c#.net dll in vc++6 programmes. So far i have created a dll, a tlb using the tlbexp.exe and a RES file using the rc.exe. I managed to import the tlb file in a c++ program but i cannot use any of my c# class methods.
Is it possible to use c#.net code in vc++6 and if yes how can i do that?
For instance what do i need to do in order to use these classes in vc++6:
using System;
namespace Operations
{
public class Addition
{
public Addition()
{
}
public double Add(double sum1, double sum2)
{
return sum1 + sum2;
}
}
public class Multiplication
{
public Multiplication()
{
}
public static double Multiply(double multiplier, double multiplicand)
{
return multiplier*multiplicand;
}
}
}
Is it possible to use c#.net code in vc++6 and if yes how can i do that?
For instance what do i need to do in order to use these classes in vc++6:
using System;
namespace Operations
{
public class Addition
{
public Addition()
{
}
public double Add(double sum1, double sum2)
{
return sum1 + sum2;
}
}
public class Multiplication
{
public Multiplication()
{
}
public static double Multiply(double multiplier, double multiplicand)
{
return multiplier*multiplicand;
}
}
}
have you tried calling RegAsm ??
I'm having a similar problem. This is what I did:
- Make sure you are compiling the DLL with the Com interop flag set to true (right click on the solution, pick properties, Configuration properties, build)
- Then I did this: http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B815808
- Add this to your C++ file:
#import "PATH\DLLNAME.tlb"
using namespace DLLNAMESPACE;
You'll then be able to see all your stuff from C++.
- Make sure you are compiling the DLL with the Com interop flag set to true (right click on the solution, pick properties, Configuration properties, build)
- Then I did this: http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B815808
- Add this to your C++ file:
#import "PATH\DLLNAME.tlb"
using namespace DLLNAMESPACE;
You'll then be able to see all your stuff from C++.
Also in your C# you need to have something like this:
namespace MyNamespace
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public interface IManagedInterface
{
bool myFunction(string spName, string connectionString);
}
public class Class1:IManagedInterface
{
public bool myFunction(string spName, string connectionString)
{
// function stuff here
}
From C++ do this:
::CoInitialize(NULL);
_Class1Ptr p;
p.CreateInstance("Mynamesp ace.Class1 ");
IManagedInterfacePtr t = p.GetInterfacePtr();
t->executeStoredProcedure( "test", "test");
CoUninitialize();
Put those two comments together and it should work just fine. I just this minute got this working myself.
namespace MyNamespace
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public interface IManagedInterface
{
bool myFunction(string spName, string connectionString);
}
public class Class1:IManagedInterface
{
public bool myFunction(string spName, string connectionString)
{
// function stuff here
}
From C++ do this:
::CoInitialize(NULL);
_Class1Ptr p;
p.CreateInstance("Mynamesp
IManagedInterfacePtr t = p.GetInterfacePtr();
t->executeStoredProcedure(
CoUninitialize();
Put those two comments together and it should work just fine. I just this minute got this working myself.
ASKER
I did what you said bluedaisydawg and i can see now my class methods but when i am running the program i always get an exception:
Debug Error!
Program: program path
Abnormal program termination
That happens when the program reaches the following line:
t->executeStoredProcedure( "test", "test");
or :
a = t->Add(1, 1); in my code
The problem seems to be the interface which is never implemented and in fact remains NULL during the whole process. From what i understand the "p.CreateInstance("Operati ons.Additi on");" fails to create the instance, and when the program tries to excecute the "t->Add(1, 1)" it crashes cos it doesn't know what to do with it...
i am posting my code in case i did something wrong and i cannot see it...
//
// C#.NET code
//
using System;
namespace Operations
{
public interface IManagedInterface
{
double Add(double sum1, double sum2);
}
public class Addition:IManagedInterface
{
public Addition()
{
}
public double Add(double sum1, double sum2)
{
return sum1 + sum2;
}
}
}
//
// VC++6 code
//
#include "stdafx.h"
#import "Operations.tlb"
using namespace Operations;
int main(int argc, char* argv[])
{
double a = 0;
::CoInitialize(NULL);
_AdditionPtr p;
p.CreateInstance("Operatio ns.Additio n");
IManagedInterfacePtr t = p.GetInterfacePtr();
a = t->Add(1, 1); // It crashes here...
CoUninitialize();
printf("%d", a);
getchar();
return 0;
}
Debug Error!
Program: program path
Abnormal program termination
That happens when the program reaches the following line:
t->executeStoredProcedure(
or :
a = t->Add(1, 1); in my code
The problem seems to be the interface which is never implemented and in fact remains NULL during the whole process. From what i understand the "p.CreateInstance("Operati
i am posting my code in case i did something wrong and i cannot see it...
//
// C#.NET code
//
using System;
namespace Operations
{
public interface IManagedInterface
{
double Add(double sum1, double sum2);
}
public class Addition:IManagedInterface
{
public Addition()
{
}
public double Add(double sum1, double sum2)
{
return sum1 + sum2;
}
}
}
//
// VC++6 code
//
#include "stdafx.h"
#import "Operations.tlb"
using namespace Operations;
int main(int argc, char* argv[])
{
double a = 0;
::CoInitialize(NULL);
_AdditionPtr p;
p.CreateInstance("Operatio
IManagedInterfacePtr t = p.GetInterfacePtr();
a = t->Add(1, 1); // It crashes here...
CoUninitialize();
printf("%d", a);
getchar();
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nope... p is still NULL... Does it work at your pc?
yes - the one i have is working... try this:
go to Programs->Microsoft Visual Studio .NET -> Visual Studio.NET tools ->Visual Studio .NET Command Prompt
Then type
regasm PATH\LIBNAME.dll
go to Programs->Microsoft Visual Studio .NET -> Visual Studio.NET tools ->Visual Studio .NET Command Prompt
Then type
regasm PATH\LIBNAME.dll
ASKER
Ok it worked. In fact it should work before as well but instead of having
p.CreateInstance(__uuidof( Addition)) ;
i had
p.CreateInstance(__uuidof( _Addition) ); ....
Thx for the help bluedaisydawg.
By the way, do you know if it is possible to use c#.net static methods as well???
p.CreateInstance(__uuidof(
i had
p.CreateInstance(__uuidof(
Thx for the help bluedaisydawg.
By the way, do you know if it is possible to use c#.net static methods as well???