0

I have written a dll in cpp, successfully built but having some issues when trying set a value to string pointer.

My codes are as follow:

my sample usage of this dll in cpp

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "OCRv1Dll.h" 

using namespace std;

int main()
{
    char *strIn = "abcd";
    char *strOu = "";
    int abc = autoOCR(strIn, strOu);
    return 0;
}

My dll's body

// ocrv1dll.cpp : defines the exported functions for the dll application.
//
//#ifdef _MSC_VER
//#define _CRT_SECURE_NO_WARNINGS
//#endif

#include "stdafx.h"


__int32 __stdcall autoOCR(char* strIn, char* strOut)
{
__int32     intRtn = 6; 
printf("Received string %s\n", strIn);
strOut += 17;
string temp = "abcd";
strcpy_s(strOut, 16, temp.c_str());
return intRtn;
}

Error has occured at

strcpy_s(strOut, 16, temp.c_str());

saying access violation memory location ...

Could you please enlighten me on this issue? thanks in advance!!

  • 2
    `strOut` doesn't point to any place you can write to. Nothing to do with DLLs. – juanchopanza Jan 11 '15 at 08:42
  • 2
    Your problem is about string manipulating. – Emadpres Jan 11 '15 at 08:46
  • I guess you think `strOut += 17` increases the size of the output buffer, however it doesn't. (There is no way to increase the size of string literals). – M.M Jan 11 '15 at 09:51
  • possible duplicate of [difference between string pointer and string array](http://stackoverflow.com/questions/27669811/difference-between-string-pointer-and-string-array) – Ulrich Eckhardt Jan 11 '15 at 10:03

2 Answers2

2

char *strOu = ""; is a pointer to an empty string (char array of length 1).

When, in the function, you write strOut += 17; , that advances the pointer by 17 characters. Now the pointer is pointing into the wilderness . It's likely that this is in a read-only data area which is why the call to strcpy_s causes an access violation.

To fix this you need to only write to memory that has been correctly allocated. You will need to design a contract between this function and its caller; for example specify that the caller must pass a writable buffer of at least a particular size.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Lovely! Thanks for your clear explanation!! <3. It worked thanks a lot. It is meant to be called from C#. Already have working code in C# but wasn't sure how to implement it in C++ since the C# functions couldn't be found in C++ and hence the confusion (in C# IntPtr pBufferOut = Marshal.AllocHGlobal(iStringBufferSize) , autoOCR(pBufferIn, pBufferOut) ) – FailedMathematician Jan 11 '15 at 10:19
0

The problem strOu might point to read only section of your program, so if you try to write to it, it will generate a memory violation error (segfault in Unix systems).

You can read more about it in this question as well: String literals: Where do they go?

What you need to do is to pass as an argument a memory location that can be written into and that has enough space to store the string you want to generate.

Try changing the definition of strOut as follows:

int main()
{
    char *strIn = "abcd";
    char strOu[20]; // just enough to hold the string
    int abc = autoOCR(strIn, strOu);
    return 0;
}
Community
  • 1
  • 1
Arturo Sevilla
  • 227
  • 1
  • 8