C++ og C# DLL
Hejsa :)Jeg er ved at skrive en "foreign routine" (C++ dll) til Saber Simulator.
Saber Simulator tager imod en C++ DLL hvis den har denne header:
extern "C" _declspec(dllexport) void X2_X3(
double* in,long* nin,double* ifl,long* nifl,double* out,
long* nout,double* ofl,long* nofl,double* undef,long* ier)
Saber Simulator bruger sproget MAST til at køre min C++ DLL.
Jeg kan godt få Saber til at vise en INTEGER hvis jeg returnerer den fra min C++ DLL, problemet er bare når jeg vil returnere data fra min C# DLL som min C++ dll er kodet sammen med. Så bliver output altid lig med 0.
Her er min kode:
C++ DLL:
#import "C:\Users\test\Desktop\SaberDllProjekt\DatabaseDLL\bin\Debug\DatabaseDLL.tlb" raw_interfaces_only
using namespace DatabaseDLL;
extern "C" _declspec(dllexport) void X2_X3(
double* in,long* nin,double* ifl,long* nifl,double* out,
long* nout,double* ofl,long* nofl,double* undef,long* ier)
{
double input= in[0];
HRESULT hr = CoInitialize(NULL);
iDatabaseDLLPtr piDatabaseDLL(__uuidof(ManagedDatabaseDLL));
double result;
hr = piDatabaseDLL->HentDatabaseDLL(input, &result);
CoUninitialize();
out[0] = result;
}
C# DLL:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Runtime.InteropServices;
namespace MinDatabaseDLL
{
public interface iDatabaseDLL
{
double HentDatabaseDLL
(
double MotorID
);
}
public class ManagedDatabaseDLL : iDatabaseDLL
{
public double HentDatabaseDLL
(
double MotorID
)
{
double motorID = 0;
double tempMotorID = 0;
SqlConnection thisConnection = new SqlConnection(@"udkommenteret;");
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "SELECT * FROM Motor.dbo.MotorData WHERE MotorID = @MotorID";
thisCommand.Parameters.AddWithValue("@MotorID", MotorID);
thisConnection.Open();
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
try
{
tempMotorID = (thisReader.GetInt32(thisReader.GetOrdinal("MotorID")));
}
catch (SqlNullValueException)
{
}
}
motorID = tempMotorID;
thisConnection.Close();
return motorID;
}
}
}