Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 954
  • Last Modified:

Unmanaged Visual c++ Memory Access Violation with C External Library

Hi all,

I'm devoloping an unmanaged visual c++ application (Win32) which uses an external dll written in C.

The application compiles successfully but it immediately throws a Memory Access Violation Error (0xC0000005:) at the first call to the external library.
The application is used to access a Loquendo Text To Speech Engine.

Here there is the code which makes references to the external code:

extern "C"
{
      #include "C:\Program Files (x86)\Loquendo\LTTS7\include\loqtts.h"
}
extern "C"
{
      #include "C:\Program Files (x86)\Loquendo\LTTS7\include\loqtypes.h"
}

......

   ttsHandleType hReader;      // Reader handle
   //Initializes a LoquendoTTS Reader using the implicit session
   ttsNewReader(&hReader, NULL);


Thanks to everybody,
Best Regards,
Danilo Pompei


0
GiovanniSV
Asked:
GiovanniSV
  • 9
  • 7
1 Solution
 
Xper4netCommented:
Can you post these header files please?
0
 
GiovanniSVAuthor Commented:
Of course, here they are:

Thank you,
Danilo Pompei
loqtts.h
loqtypes.h
0
 
Xper4netCommented:
Are you sure that you can let the second parameter as NULL without creating at least one session with ttsNewSession?
I think that code should be as demonstrated on http://www.voip-info.org/wiki/view/Loquendo+TTS :

ttsHandleType hSession, hReader; /* Reader handle */ 
ttsResultType r; /* Error code returned by TTS APIs */ 

/* Initializes a LoquendoTTS Reader using the implicit session */ 
r = ttsNewSession(&hSession,"/opt/Loquendo/LTTS7/bin/default.session"); 
if (r != tts_OK) { 
fprintf(stderr, "%s\n", ttsGetErrorMessage(r)); 
ttsDeleteSession(NULL); /* deallocates all tts resources */ 
return r; 
} 

r = ttsNewReader(&hReader, hSession); 
if (r != tts_OK) { 
fprintf(stderr, "%s\n", ttsGetErrorMessage(r)); 
ttsDeleteSession(NULL); /* deallocates all tts resources */ 
return r; 
}

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
Xper4netCommented:
Note that you don't have to use extern "C" for inclusion, as these header already embed this preprocessor directive when necessary. Just remove them.
0
 
GiovanniSVAuthor Commented:
Thank you for your help.

Yes I'm sure it can be called with a null parameter. I have the same piece of code in a C program which calls this method with a null second parameter and it runs smoothly. I also tried in C++ to pass a not null second parameter and it doesn't work either. In C everything seems to run ok but in C++ it doesn't work at all.

I removed the extern C as you suggested but it has no effect.

I would like also to notify that when I build for Release in Visual C++ it fails with a LNK2001 error. The linker doesn't seem to find the library I attached to the process. Everything builds fine when I build in Debug mode.

Thank you again for your help,
Best Regards,
Danilo Pompei
0
 
Xper4netCommented:
Ok. So we have to check compilation options.
Please attach your project file (only the project, other sources are not necessary).
0
 
GiovanniSVAuthor Commented:
Here there is the project attached.

Previously I didn't mention the fact that in Debug modes it builds fine but that it doesn't run and results in the Memory Access error I talked about.

In Release mode it doesn't build.

The thing that I did in Debug mode was to add the LoqTTS7.lib to the AdditionalDependencies and the LoquendoDLL directory to the AdditionalLibraryDirectories. I cannot find where to add it in Release mode.

I'm sorry for this confusion I have made but this is my first project in Visual C++ I always worked in Visual C#.

Thank you,
Best Regards,
Danilo Pompei
TEST-TTS-C-WIN32.vcxproj.txt
0
 
Xper4netCommented:
Ok, so you're using VS2010. I dont have it on my machine, so I'm not sure if it defines _WIN32 or not, as required by your library.
Try to add these lines in one of your source file:
#ifdef _WIN32
#pragma message("_WIN32 is defined")
#else
#pragma message("_WIN32 is not defined")
#endif
Then look at your logs when you compile. If it's not defined, just add it in preprocessor list (Project|Properties|C/C++|Preprocessor definitions : add ";_WIN32" to existing list)
To do the same that you've done in release configuration, just change configuration in the left top combobox of project properties.
0
 
GiovanniSVAuthor Commented:
It seems _WIN32 is defined.

As for the other subject I changed the properties in my Release configuration and now it builds but it gives the same Memory Access Violation Error.

Thank you,
Best Regards,
Danilo
0
 
Xper4netCommented:
Ok. As I dont see anything else in your project options which could lead to this issue, the las thing that I could do is take a look at your whole calling code. Please attach TEST_TTS_C_WIN32.cpp.
0
 
GiovanniSVAuthor Commented:
Here is the code. There is nothing added except for Local Variables identified by my comment and a linke in the main function also identified by a comment of mine.

Thank you,
Danilo
TEST-TTS-C-WIN32.cpp
0
 
Xper4netCommented:
There's nothing wrong in your code. So I suppose there's an incompatibility between the C-Runtime used by your library and the one used by VS2010. Or maybe you link the wrong library.
Are you sure that LoqTTS7.lib is the link DLL library version or the static one (what's its size)?
It seems indeed that this library exists in a static library version. If you're not wrong with the DLL lib, then you can try it if you have. As i see in library's header, you just have to add ";INCLUDE_STATIC_LIB" in your preprocessor definitions, and replace the link library name with the static version name.
0
 
GiovanniSVAuthor Commented:
I've got a LoqTTS7.lib file which is 73KB and a LoqTTS7.dll which is 746 KB.

I think that the lib version is used in my project. I don't know how to add a DLL to the project.

The Add Reference menu in Visual C++ allows me only to add Project References and not external dll.

Thank you,
Danilo
0
 
Xper4netCommented:
In C++, to link a DLL, you just have to link the .lib provided with the DLL. Some components are delivered as static library (a .lib too), but in this case, this .lb is bigger, that's why I asked you the library size.
In your case, you have correctly linked the good .lib, and the corresponding DLL seems to be reachable at execution time (otherwise you would have a different error message).
So, my only supposition is that your DLL is not compatible with the C++ project generated by VS2010. Maybe a solution exists, but some attempts are necessary.
If you can post both LoqTTS7.lib and LoqTTS7.dll, I could do some quicks tests.
0
 
GiovanniSVAuthor Commented:
Unfortunately I cannot post the library because of confidentiality reasons.

It would also be useless because the library is licensed and bound to my mac address.

I'm sorry,
Thank you,
Danilo
0
 
Xper4netCommented:
Ok, so the last idea that I have is the following :

In Project|Properties|C/C++|Runtime Library, choose "Multi threaded Debug /MTd" in debug configuration and "Multi Threaded /Mt" in release configuration.

It's avoid using of C++ DLL runtime Library.

If it doesn't work, let me know, you should try to see by yourself what are your DLL dependencies.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now