C++ to C# help (thanks)

L

llihp

Hi all, I'm new to C#, so apologies if this is a dumb question. smile

I'm trying to convert some code from C++ to C# so I can write some DLL
files.

Here is the original code in C++, firstly here is the header file:


Code:

// the interface structure
struct frontEndInterface {
short (* getParamCount) ();
short (* getReturnCount) ();
void ( * popInteger) (long & , short & );
void ( * pushInteger) (const long , short );
void ( * popString) (char *, short &, short &);
void ( * pushString) (const char *,short , short );
void ( * getFrontEndEnv)(const char *, char *, short &);
};

// a small macro used to declare the "exportable" functions
#define EXPORT __declspec(dllexport)

// the functions we want to be available from the front end
EXPORT void __stdcall initialize();
EXPORT void __stdcall finalize();

EXPORT int __stdcall myFunc(const struct frontEndInterface &fx);


Here is the .cpp file:

Code:

#include <stdio.h>
#include <sys/stat.h>


void __stdcall initialize() {

}

//
------------------------------------------------------------------------
--
/* the entry of the DLL. This function is the main function of a DLL and
is called when the DLL is loaded by the system. This is specific to
Windows systems */
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
return TRUE;
}


int __stdcall myFunc(const struct frontEndInterface &fx) {
return 1;
}


void __stdcall finalize() {

}


I've cut out all the extra code and just left the bare bones.

Here's my C# attempt:

Code:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

unsafe public struct frontEndInterface {
unsafe public extern short *getParamCount();
unsafe public extern short *getReturnCount();
unsafe public extern void *popInteger(ref long a, ref short b);
unsafe public extern void *pushInteger(long a, short b);
unsafe public extern void *popString(char *a, ref short b, ref short
c);
unsafe public extern void *pushString(char *a, short b, short c);
unsafe public extern void *getFrontEndEnv(char *a, char *b, ref
short c);
unsafe public extern void *popWString(char *a, ref short b, ref
short c);
unsafe public extern void *pushWString(char *a, short b, short c);
};

[Guid("E9B5EFB6-7AF4-41D1-AAC7-9AD909E8D6E1"), InterfaceType
(ComInterfaceType.InterfaceIsIDispatch)]
public interface _SimpleDll {
[DispId(1)] void initialize();
[DispId(2)] void finalize();
[DispId(3)] int myFunc(ref frontEndInterface fx);
}

[Guid("99DA9C29-2F53-4BAD-B621-5109060D6FE8"), ClassInterface
(ClassInterfaceType.None), ProgId("ConvertToPDF")]
class SimpleDll : _SimpleDll
{
public void initialize() {
}

public void finalize() {
}

public int myFunc(ref frontEndInterface fx){
return 1;
}
}


The C# code compiles ok, but my program cannot seem to use the dll.

Any help appreciated!
 
N

Nicholas Paldino [.NET/C# MVP]

llhip,

Interfaces are used only through COM interop (at least in the scope of
using interfaces as a bridge between managed and unmanaged code).

What you have to do in this case is use a DllImport attribute in order
to call the functions that you are exposing from a DLL.

If these are not functions you are exposing, and it is an actual
interface on a C++ object, then you will have to use managed extensions for
C++ to access it, or expose the object through COM and expose it through COM
interop.

Hope this helps.


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

llihp said:
Hi all, I'm new to C#, so apologies if this is a dumb question. smile

I'm trying to convert some code from C++ to C# so I can write some DLL
files.

Here is the original code in C++, firstly here is the header file:


Code:

// the interface structure
struct frontEndInterface {
short (* getParamCount) ();
short (* getReturnCount) ();
void ( * popInteger) (long & , short & );
void ( * pushInteger) (const long , short );
void ( * popString) (char *, short &, short &);
void ( * pushString) (const char *,short , short );
void ( * getFrontEndEnv)(const char *, char *, short &);
};

// a small macro used to declare the "exportable" functions
#define EXPORT __declspec(dllexport)

// the functions we want to be available from the front end
EXPORT void __stdcall initialize();
EXPORT void __stdcall finalize();

EXPORT int __stdcall myFunc(const struct frontEndInterface &fx);


Here is the .cpp file:

Code:

#include <stdio.h>
#include <sys/stat.h>


void __stdcall initialize() {

}

//
------------------------------------------------------------------------
--
/* the entry of the DLL. This function is the main function of a DLL and
is called when the DLL is loaded by the system. This is specific to
Windows systems */
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
return TRUE;
}


int __stdcall myFunc(const struct frontEndInterface &fx) {
return 1;
}


void __stdcall finalize() {

}


I've cut out all the extra code and just left the bare bones.

Here's my C# attempt:

Code:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

unsafe public struct frontEndInterface {
unsafe public extern short *getParamCount();
unsafe public extern short *getReturnCount();
unsafe public extern void *popInteger(ref long a, ref short b);
unsafe public extern void *pushInteger(long a, short b);
unsafe public extern void *popString(char *a, ref short b, ref short
c);
unsafe public extern void *pushString(char *a, short b, short c);
unsafe public extern void *getFrontEndEnv(char *a, char *b, ref
short c);
unsafe public extern void *popWString(char *a, ref short b, ref
short c);
unsafe public extern void *pushWString(char *a, short b, short c);
};

[Guid("E9B5EFB6-7AF4-41D1-AAC7-9AD909E8D6E1"), InterfaceType
(ComInterfaceType.InterfaceIsIDispatch)]
public interface _SimpleDll {
[DispId(1)] void initialize();
[DispId(2)] void finalize();
[DispId(3)] int myFunc(ref frontEndInterface fx);
}

[Guid("99DA9C29-2F53-4BAD-B621-5109060D6FE8"), ClassInterface
(ClassInterfaceType.None), ProgId("ConvertToPDF")]
class SimpleDll : _SimpleDll
{
public void initialize() {
}

public void finalize() {
}

public int myFunc(ref frontEndInterface fx){
return 1;
}
}


The C# code compiles ok, but my program cannot seem to use the dll.

Any help appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top