0

I'm trying to invoke a DLL function that requires Asciiz pointers:

int doExample(Asciiz* FolderPath, Asciiz* OutputPath, int Flags);

I've tried using StringBuilder, but I keep getting errors about accessing protected memory. This is my C# code:

public class Example
{
  [DllImport("Example.dll", EntryPoint="?doExample@Example@@QADHPYDH0P6A_N0ZZ000@Z")]
        static extern int doExample(StringBuilder folderPath, StringBuilder outputPath, int flags);

  public int do(string folderPath, string outputPath)
  {
    StringBuilder FolderPath = new StringBuilder(folderPath);
    StringBuilder OutputPath = new StringBuilder(outputPath);
    doExample(FolderPath, OutputPath, 0);
  }
}

Any ideas as to why this would be happening? Or would this strictly be an error in the C++ code?

Edit: based on the comments, here is so more info:

  • The DLL is built on VS2010
  • I am building the C# code in VS2012 (could the difference here be causing an issue?)
  • I don't know exactly what the function in the DLL does; my knowledge in C++ is very limited. Basicly, it gets a directory, compresses it and outputs it to the outputpath in a single file.
  • I know the name mangling in the EntryPoint is incorrect; I've put some random characters in; the function name is not the same as it is in the actual DLL, but argument types and return type is the same. Also, I got the real mangled function name from Dependency Walker. If I didn't specify the EntryPoint, it couldn't find the function. (I do have the C++ source code btw of this DLL)
  • The full error is: AccessViolationException was unhandled: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
4
  • That function name is not one that the MSVC++ compiler could generate although the name mangling is pretty similar. You have to be specific about the native compiler you use. The missing CallingConvention is notable, almost always Cdecl for C++ functions. Non-static instance methods cannot be pinvoked. Dec 1, 2013 at 19:26
  • How is Asciiz defined? What is the calling convention of your function? Why can't we see the error message? Is the function really modifying both strings? Dec 1, 2013 at 21:31
  • I have specified more information in the question now. Dec 2, 2013 at 11:50
  • You did not answer all the questions. How is Asciiz defined? What is the calling convention of your function? Is the function really modifying both strings? And a new one. What is the true mangled name? If you told us that we could decode it into a signature! That would help you! We can certainly help you, but you've got to give more information. Dec 2, 2013 at 21:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.