日期:2014-05-18  浏览次数:20969 次

尝试读取或写入受保护的内存。这通常指示其他内存已损坏。问题
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"
#include "mfcpch.h"
#include "allheaders.h"
#include "baseapi.h"
#include "strngs.h"
#include "params.h"
#include "blobs.h"
#include "notdll.h"

extern "C" __declspec(dllexport) int donetapi(char * imagepath,char * output, char * lang, int psm)
{
tesseract::PageSegMode pagesegmode = tesseract::PSM_AUTO;

pagesegmode = static_cast<tesseract::PageSegMode>(psm);

tesseract::TessBaseAPI api;

api.Init(imagepath, lang, tesseract::OEM_DEFAULT,
NULL, 0, NULL, NULL, false);

if (api.GetPageSegMode() == tesseract::PSM_SINGLE_BLOCK)
api.SetPageSegMode(pagesegmode);

FILE* fin = fopen(imagepath, "rb");

if (fin == NULL) {
return -1;
}

fclose(fin);

PIX *pixs;
if ((pixs = pixRead(imagepath)) == NULL) {
return -1;
}

pixDestroy(&pixs);

STRING text_out;
if (!api.ProcessPages(imagepath, NULL, 0, &text_out)) {
return -1;
}

STRING outfile = output;
   
FILE* fout = fopen(outfile.string(), "wb");
  if (fout == NULL) {
  return -1;
}
fwrite(text_out.string(), 1, text_out.length(), fout);
fclose(fout);
   
return 0;  
}

BOOL APIENTRY DllMain( HMODULE hModule,
  DWORD ul_reason_for_call,
  LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

============================
以上是 tesseract 开源的文字识别的软件源码里面添加的,我是把这个生成了DLL,然后在C#里面调用的。

C#里面:

[DllImport("tessdll.dll")]
  public unsafe static extern int donetapi(string imagepath, string output, string lang, int psm);

每次调用的时候,就老是提示这个问题,尝试读取或写入受保护的内存。这通常指示其他内存已损坏。偶尔能够顺利通过。

------解决方案--------------------
C# code

 /// Return Type: int
    ///imagepath: char*
    ///output: char*
    ///lang: char*
    ///psm: int
    [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="donetapi")]
public static extern  int donetapi(System.IntPtr imagepath, System.IntPtr output, System.IntPtr lang, int psm) ;