I have been trying to call a C++ function in a DLL, from a C# WPF application I'm working on. I've done it before and it's worked but I can't remember how I did it and can't seem to find an example that works for me.
I'm doing something wrong somewhere but I can't work out where.

I've created a project that produces a DLL in C++ with /clr set, and then created a C# WPF project added a button and then added the DLL reference to the WPF project.
Both projects and in the same solution, the DLL compiles first.
I can't seem to access functions within the DLL and somehow I remember there being both a DLL and a .lib last time. I don't know whether I'm confusing with another project.

If anyone has experience with this issue or feels they know where I'm going wrong please could you tell me about it.

Thank-you!

Did you make managed C++ code or unmanaged C++ code?

If you made unmanaged code, you will probably need to wrap that in managed code.

A little article on the MSDN about it

Otherwise, just linking it like you have should be enough...

I've got /clr set on my C++ DLL.

According to this Microsoft article /clr may not be sufficient to make a managed-code DLL. Be sure to read the Remarks section of that aricle carefully.

The /clr option does not imply that classes, interfaces, or structs are managed; use __gc to explicitly specify when you want a construct to be managed

IMHO it would have been a lot easier to have just created a CLR/C++ DLL so that VS can set all flags correctly.

After messing around last night with deleting and re creating the DLL project I've only managed to get it to work through the use of

[DllImport("Network.dll")]
static extern int function();

I wish to use the functions in the DLL without having to DllImport if possible and definately do not wish to declare every function prior to calling it.

Create a header file that contains all the function prototypes, which is how most DLLs do it. You can use the same header file for both the DLL and application program by creating a macro that is defined to be __dllexport in the DLL and __dllimport in the application program. This walkthrough will teach you how it's done. You can export entire classes like that if you want to.

How would I go about including the header in a C# project

Just wrap around it using P/Invoke like you have done. It's a perfectly viable solution, think of it as just declaring an interface :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.