I want to compile a c++ program to a dll and use it from c#.
This is the c++ program:
I changed the header and the method declaration in the h and c++ file from:
void MurmurHash3_x64_128 ( const void * key, int len, uint32_t seed, void * out );
to
extern "C" __declspec(dllexport) void MurmurHash3_x64_128
(const void * key, const int len, const uint32_t seed, void * out )
I did the same for all three methods. Then I set the compile target to dll. After compiling it I had a x64 bit dll named SMHasher.dll. Now I created a new x64 bit C# programm and used this dll as reference.
Then I wrote the following:
[DllImport("SMHasher.dll")]
public static extern void MurmurHash3_x64_128(byte[] valueToHash, int len, uint seed, out byte[] hash);
private void button1_Click(object sender, EventArgs e)
{
byte[] hash;
MurmurHash3_x64_128(new byte[] { 1, 2, 3 }, 3, 0, out hash);
}
When calling MurmurHash3_x64_128 my test app gets closed without any error message.
What is the problem? And how can I solve it?
Maybe the problems are the c++ parameters? Maybe “const void * key” is not a byte array?
This error is occurring because you are a 64-bit app importing a 32-bit dll, or a 32-bit app importing a 64-bit dll.
Make sure you LoadLibrary a dll of the same bittedness of your app.
In C#, you can set the bittedness of your app through the use of the configuration properties in Visual Studio.