2

This is my C# code:

    [DllImport("Tomb.dll")]
    public static extern unsafe uint InjectManualMap(ulong pId, [MarshalAs(UnmanagedType.LPCStr)]string dllPath);

and for whatever reason when I attempt to use my C++ code:

extern "C" DllExport unsigned int StdCall InjectManualMap(unsigned long pid, const char* dllPath) {
     ManualMapInjector* mmp = new ManualMapInjector;
     std::string dll(dllPath);
     unsigned int kk = mmp->Inject(pid, dll);
     delete mmp;
     return kk;
}

the const char* dllPath is always a bad pointer (0x000000000). I'm not sure what's happening here, because all other solutions point to using either a StringBuilder (tested it, did the same thing), or using MarshalAs which is something the current code I have posted does.

jimbabwe
  • 23
  • 2
  • Out of curiosity, what happens when you keep `string`, but remove the `MarshalAsAttribute`? The examples I saw that supposedly worked, were not using `MarshalAs`. http://stackoverflow.com/questions/20752001/passing-strings-from-c-sharp-to-c-dll-and-back-minimal-example – TyCobb Aug 06 '14 at 06:03
  • Exactly the same thing happens, sadly. I can't get it to work any which way. – jimbabwe Aug 06 '14 at 06:06
  • Could be an issue with what you are passing in for your `dllPath`. There were some decent suggestions on this page that I didn't see mentioned elsewhere -- http://manski.net/2012/06/pinvoke-tutorial-passing-strings-part-2/ – TyCobb Aug 06 '14 at 06:17

1 Answers1

1

The issue is that C# long is 64 bits wide, but C++ long, on Windows, is 32 bits wide. The pinvoke should be:

[DllImport("Tomb.dll")]
public static extern uint InjectManualMap(uint pId, string dllPath);

Note that I also removed the unsafe directive and the MarshalAs attribute which are not needed.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks. You might like to accept the answer. I know you are new here which is why I mention it. – David Heffernan Aug 06 '14 at 07:49
  • 1
    I didn't realize where the button was! I come to stackoverflow all the time, but I never registered until now. Thanks again for your help! :) – jimbabwe Aug 06 '14 at 08:49