How export old C dll for use in C# Class Library

There are two old C files with tow related headers.

For using them in Visual Studio 2013 i created a Visual C++ Win32 Console Application and disabled precompiled headers from properties of project.

I deleted stdafx.h & targetver.h & dllmain.cpp & project_name.cpp(created by vs).

In Header Files area i added two files And in Source Files area i added another tow files.

My project is look like this :
i.stack.imgur.com/SjJ7e.png

Here is my entire project's link :
rodfile.com/alaeux6ozhbw

Consider on .c file extensions(no .cpp).

In header files there is no class usage and you will see many structs in there because of old c language.

Now i want to export this project's dll for use in a simple console application c# project.

How can do that?

I put this peace of code in the upper of both header files :

__declspec(dllexport) int main();

int main() function is inside
wmm_file.c
file and this main function uses many functions in another c file (GeomagnetismLibrary.c).

I want to use all functions of c++ project in c# code.

Here is my c# codes (Prpogram.cs) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Magnetic_Model_CSharp
        
            [DllImport(@"Y:\Magnetic_Model_C_dll_2.dll", CallingConvention = 
CallingConvention.Cdecl)]
            public static extern int main();
    
            static void Main(string[] args)
            {
                main();
            }
        }
    }


And here is the error after run :

An unhandled exception of type 'System.AccessViolationException'
occurred in Magnetic_Model_CSharp.exe


How can i fix this error and make that c(c++) project exportable?

As my friends said you shouldn't use main keyword so i renamed it.

For a test i created a simple
Declaration :

int Add(int a, int b); /* PUT IT ON GeomagnetismHeader.h FILE*/

Definition :

1
2
3
4
5
6
    int Add(int a, int b) /* PUT IT ON wmm_file.c FILE >
                    I RENAMED main() TO main_____() IN THIS FILE*/
    {
    	printf("Hello Joy!\r\n");
    	return a + b;
    }


And renamed both :

__declspec(dllexport) int main();
To
__declspec(dllexport)

In both header files.

Now the problem is the position of declaration of int Add(int a, int b); function in GeomagnetismHeader.h file.

When i put it before typedef struct { it works.

But when i put it after typedef struct { does not work and all of my declarations are after typedef struct {.

Any idea?
Last edited on
Topic archived. No new replies allowed.