473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Calling C/C++ Dll from CSharp

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
Nov 17 '05 #1
2 5495
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]
- mv*@spam.guard.caspershouse.com

"SS" <sa**********@verizon.net> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
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

Nov 17 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Oq**************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"SS" <sa**********@verizon.net> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
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


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: The_King | last post by:
Hello all: I was wondering if anyone has tried (and got to work) calling a c#.net ..dll from VBA code? I have been working on this for about 2 weeks and am not getting any where. The_King
2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
2
by: Pawan Aggarwal | last post by:
I'm having trouble with calling an exported function in a native DLL compiled with eMbedded Visual C++ in C# application in PocketPC 2002 OS. Problem Description follows: I have one exported...
1
by: Paul J Barrett | last post by:
I have a problem with the marshaller calling a vb6 dll from csharp. here follows the code that is similar in structure to what i want to call. rem vb6 dll type a b1 as byte b2(2) as byte...
6
by: Dave R | last post by:
Hello group, I had a COM dll in VC 6.0 which my Perl 5.8 modules used to call. With the .Net invent I would like to wrap up my COM functionality into C#. I wanted to know how can my Perl modules...
1
by: NOSPAM | last post by:
I have a question on calling parent class... What I want to do is: when calling the derived class, it automatically executes the parent's method, is it possible? // main code: Derived d = new...
3
by: RC | last post by:
Hi, I have a dll written by vb6. AFunction(ByRef o as Variant) I want to use it in csharp by System.Reflection. Assembly asm = Assembly.LoadFrom("Lib.dll"); Type t =...
0
by: ajitgoel | last post by:
Hi; We have a Javascript function which I have been tasked to move to a CSharp class. This javascript function uses Regular expression extensively. This function has a string input parameter and...
6
by: Ajit Goel | last post by:
Hi; We have a Javascript function which I have been tasked to move to a CSharp class. This javascript function uses Regular expression extensively. This function has a string input parameter and...
0
by: Mike | last post by:
I have a new installer that allows the same product to be installed multiple times on the same machine. I also have a configuration program in .NET 2.0 SP1. I would like the config program to be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.