[C#] C dll import in C#

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Ik moet een C# API schrijven rond een dll geschreven in C, dankzij een knowledge-transfer ( of /verlies is meer het woord) van onze vroegere software-vendor naar ons is hierover bitter weinig documentatie over te vinden. Via dllexp heb ik de ingebakken functies teruggevonden en via documentatie uit een duister hoekje heb ik nu eindelijk de return types en de parameters.
Tot hier geen problemen, echter als ik de functies aanroep krijg ik errors in het genre van "unable to find an entry point named 'cis_getConnectedPKIuser' in DLL C:\\...." |:(


de code :
C#: test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using System.Runtime.InteropServices;


namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
internal static class unsafeNativeMethods 
{ 
const string _dllLocation = "C:\\Program Files\\TestAPI\\TestAPI_Module.dll"; 
[DllImport(_dllLocation)] 
public static extern void cis_beginSession(); 
[DllImport(_dllLocation)] 
public static extern void cis_getConnectedPKIuser(); 

} 
public Form1() 
{ 
InitializeComponent(); 
} 

private void cmd_cis_beginSession_Click(object sender, EventArgs e) 
{ 
unsafeNativeMethods.cis_beginSession(); 
//txt_cis_beginSession.Text = "started"; 

} 
private void 
cmd_cis_getConnectedPKIuser_Click(object sender, EventArgs e) 
{ 
unsafeNativeMethods.cis_getConnectedPKIuser(); 
//txt_cis_getConnectedPKIuser.Text = "1"; 
} 
} 
} 


stuk uit de DLL met de functies

C: TestAPI_Module.dll
1
2
3
4
5
6
7
8
// session management
void InitSession(SESSION* Session); 

SESSION* CALL cis_beginSession(); 

void CALL cis_endSession(SESSION* session); 

LPTSTR CALL cis_getConnectedPKIUser(SESSION* session); 



Mijn vragen :

1 : is dit de correcte manier om een C dll in C# te gebruiken

2 : hoe kan ik de entry points van de functies vinden zodat mijn compiler hier niet meer op crasht

kan iemand hierbij helpen aub? :) _/-\o_

Acties:
  • 0 Henk 'm!

  • whoami
  • Registratie: December 2000
  • Laatst online: 23:16
Ik ben er ook zo geen held in, maar ik zie dit:
code:
1
2
3
4
[DllImport(_dllLocation)]  
public static extern void cis_beginSession();  
[DllImport(_dllLocation)]  
public static extern void cis_getConnectedPKIuser();

En ik zie dit:
code:
1
2
3
4
5
6
7
void InitSession(SESSION* Session);  

SESSION* CALL cis_beginSession();  

void CALL cis_endSession(SESSION* session);  

LPTSTR CALL cis_getConnectedPKIUser(SESSION* session);

Maw, er is geen enkele method signature in die C dll die past met de methods die je in je C# programma import.
Er is geen cis_beginSession in je C dll die void als return type heeft, terwijl je deze wel zo declareert in C#
Er is geen cis_getConnectedPKIUser die void als return type heeft, en geen argumenten heeft.

[ Voor 16% gewijzigd door whoami op 01-09-2009 12:15 ]

https://fgheysels.github.io/


Acties:
  • 0 Henk 'm!

  • Soultaker
  • Registratie: September 2000
  • Laatst online: 01:47
@whoami: functies in C DLL's worden alleen geïdentificeerd op basis van naam, niet de hele signature inclusief parameters en return type, dus de foutmelding in kwestie is daar niet door te verklaren. Waarschijnlijk kun je prima die functies als void-functie importeren waarna de boel crasht at runtime, maar dan hoort het nog wel te compileren.

Wellicht is het concrete probleem veel simpeler: in de C# code importeer je getConnectedPKIuser maar in de header file (?) staat die functie als getConnectedPKIUser gedeclareert. Die symboolnamen komen dus niet overeen, want die zijn hoofdlettergevoelig.