Calling C/C++ Dll from CSharp

S

SS

Hi I have a dll with the following signature - I would like to know How I
can call this from CSharp,pls

This is a test app from C - and it works - but from CSharp???

#include <fstream.h>
#include <stdio.h>
#include <string.h>
#include "SalDef.h"


extern "C" void __stdcall SalCalculate(Date *date,Location *loc,ConfigData
*data,char *s);


int main()

{

Location loc;
ConfigData data;
Date date;
double magDec;

char output[10000]; // Make sure the length is enough. I.e. for year

// calculation you need more!

ofstream outFile;
double dir, dist;

data.imsakAngle=(float)-16.0;

data.fajrAngle=(float)-13.5;

data.ishaAngle=(float)-16.0;
data.dist=10;
data.fiqh=1;
data.precision=1;
data.addMin=0;
data.secondsToCorrect=2;
data.maxErrorInSec=1;
data.rule=1;
data.temp=10;
data.pressure=0;

data.visibleLimit=(float)15;

data.adjust=0;
data.nMinutes=90;
data.programFlags=0X100;
strcpy(loc.name,"XCYZ");

loc.lat=33.0463;
loc.lon=-96.9939;
loc.zone=(float)-6.0;
loc.saveStr=0;
loc.saveEnd=0;
loc.height=0;
date.day=1;
date.month=11;
date.year=2005;
date.isValid=0;
outFile.open("SaTest.txt");

if (outFile)
{
if(SalisValidDate(&date,0)==True)
{

SalCalculate(&date,&loc,&data,output);
outFile << output;
outFile.close();
}
}
return 1;
}

Thx

SalQ
 
N

Nicholas Paldino [.NET/C# MVP]

SS,

Without seeing the definitions for the Date, Location, and ConfigData
strutures, it is impossible to tell.

However, if you have the DLL in a location where LoadLibrary will find
it, you can declare it in C# like this:

[DllImport("your.dll", CharSet=CharSet.Ansi)]
static extern SalCalculate(ref Date date, ref Location loc, ref ConfigData
data, StringBuilder s);

Hope this helps.
 
S

SS

This is the .h header file for the dll:

//

// This file "SalDef.h" contains the structure definitions

// It is not recommended to modify this file. Please do not manipulate the
definition for unused parameters

// to avoid any corruption in data structures.

//

#ifndef SALDEF

#define SALDEF_H

#endif

#define MaxNameLength 41

#define MaxPathLength 80

#define FAlse 0

#define TRue 1

typedef struct {

int year;

int iyear;

unsigned char month;

unsigned char imonth;

double day;

double iday;

int isValid;

unsigned char hour;

unsigned char minute;

unsigned char second;

} Date;

typedef struct {

char name[MaxNameLength];

double lat;

double lon;

int height;

float zone;

int saveStr;

int saveEnd;

} Location;

typedef struct {

float imsakAngle;

float fajrAngle;

float ishaAngle;

int fiqh;

int dist;

int precision;

int addMin;

int temp;

int pressure;

int refraction;

int secondsToCorrect;


int maxErrorInSec;


float visibleLimit;

int rule;

int programFlags;

int adjust;

int nMinutes;

int timer;

int bMin;

char editor[MaxPathLength];

char soundFile[MaxPathLength];

char soundFile2[MaxPathLength];

} ConfigData;



Thx SalQ

Nicholas Paldino said:
SS,

Without seeing the definitions for the Date, Location, and ConfigData
strutures, it is impossible to tell.

However, if you have the DLL in a location where LoadLibrary will find
it, you can declare it in C# like this:

[DllImport("your.dll", CharSet=CharSet.Ansi)]
static extern SalCalculate(ref Date date, ref Location loc, ref ConfigData
data, StringBuilder s);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SS said:
Hi I have a dll with the following signature - I would like to know How I
can call this from CSharp,pls

This is a test app from C - and it works - but from CSharp???

#include <fstream.h>
#include <stdio.h>
#include <string.h>
#include "SalDef.h"


extern "C" void __stdcall SalCalculate(Date *date,Location *loc,ConfigData
*data,char *s);


int main()

{

Location loc;
ConfigData data;
Date date;
double magDec;

char output[10000]; // Make sure the length is enough. I.e. for year

// calculation you need more!

ofstream outFile;
double dir, dist;

data.imsakAngle=(float)-16.0;

data.fajrAngle=(float)-13.5;

data.ishaAngle=(float)-16.0;
data.dist=10;
data.fiqh=1;
data.precision=1;
data.addMin=0;
data.secondsToCorrect=2;
data.maxErrorInSec=1;
data.rule=1;
data.temp=10;
data.pressure=0;

data.visibleLimit=(float)15;

data.adjust=0;
data.nMinutes=90;
data.programFlags=0X100;
strcpy(loc.name,"XCYZ");

loc.lat=33.0463;
loc.lon=-96.9939;
loc.zone=(float)-6.0;
loc.saveStr=0;
loc.saveEnd=0;
loc.height=0;
date.day=1;
date.month=11;
date.year=2005;
date.isValid=0;
outFile.open("SaTest.txt");

if (outFile)
{
if(SalisValidDate(&date,0)==True)
{

SalCalculate(&date,&loc,&data,output);
outFile << output;
outFile.close();
}
}
return 1;
}

Thx

SalQ
 
Top