Jump to content

[Help] Using/calling a function from a C++.DLL in C#.

- - - - -

  • Please log in to reply
10 replies to this topic

#1
g-man122

g-man122

    Newbie

  • Members
  • PipPip
  • 29 posts
Hi people,

Recently, I've been trying--and failing-- to call a function from a C++ dynamic link library from a C# console application.. All is good, the compiling goes well on both ends, however! When I try call the function from the C++ DLL, I get this error :
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
coming from the C# console application. It'll break at the function calling part and give me that.

Here's the C# console application code.
And here's the C++ DLL code

I've googled the error many times, nobody seems to have a working solution. I'm running Windows XP Pro 64-bit.

Any help would be immensely appreciated.

Thanks for reading.

[Sorry if it's the wrong section]

#2
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
Your DLL needs DLLMain, the entry point.

http://msdn.microsof...583(VS.85).aspx
http://msdn.microsof...y/1ez7dh12.aspx


#3
g-man122

g-man122

    Newbie

  • Members
  • PipPip
  • 29 posts

outsid3r said:

Your DLL needs DLLMain, the entry point.

http://msdn.microsof...583(VS.85).aspx
DLLs

Hey, thanks for your reply.

So, my new DLL code should be :
#922135 - Pastie
Right ?

#4
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
read this also

dllexport, dllimport

#5
g-man122

g-man122

    Newbie

  • Members
  • PipPip
  • 29 posts

outsid3r said:

read this also

dllexport, dllimport

Yeah, I just remembered that.

I think I may be getting somewhere, though I'm getting the same error.
This is my code :
#922173 - Pastie
Maybe there's something I'm not seeing ?

#6
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
what compiler are you using to build the DLL?

#7
g-man122

g-man122

    Newbie

  • Members
  • PipPip
  • 29 posts

outsid3r said:

what compiler are you using to build the DLL?
Microsoft's one. (from Visual C++ 2008)

#8
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
make sure you use extern "C" both in declaration and definition of the function. Put DLLMain on top of all functions also.

#9
g-man122

g-man122

    Newbie

  • Members
  • PipPip
  • 29 posts
#include "Windows.h"
#include <iostream>

extern "C"
{
    __declspec (dllexport) int SendMsg() { 
        return 101001;
    }

BOOL WINAPI DllMain (HMODULE hModule, DWORD state, LPVOID lpReserved)    {
    if (state == DLL_PROCESS_ATTACH)    {
        MessageBoxA(NULL,"Hi","Hi",MB_OK);
        CreateThread (0,0,(LPTHREAD_START_ROUTINE)SendMsg,0,0,0);
    }
    return TRUE;
}
}

Like that ?
And I wouldn't be able to add the DllMain part at the top of the file, it won't let me declare new functions.

Thanks for your help, anyhow.

#10
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
Yes you can, you should have an header file and a source file. Header file contains the prototypes and the source file the definitions.

#11
g-man122

g-man122

    Newbie

  • Members
  • PipPip
  • 29 posts

outsid3r said:

Yes you can, you should have an header file and a source file. Header file contains the prototypes and the source file the definitions.

Could you correct my code, please ?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users