VaclavPe

Hello all,

for one my project I have to use old-fashioned DLL. I don't know why but my C# code does not work:

class Program
{
#region Dll Imports
[DllImport("FuncModuleCpp.dll", EntryPoint = "fnFuncModule_GetVersion", CharSet = CharSet.Auto)]

static extern Boolean fnFuncModule_GetVersion( StringBuilder sVersionOut,
StringBuilder sErrorOut);
#endregion

static void Main(string[] args)
{
StringBuilder stri1 = new StringBuilder(128);
StringBuilder stri2 = new StringBuilder(128);

Boolean resFuncModule = fnFuncModule_GetVersion(stri1, stri2);

if (resFuncModule)
{
Console.WriteLine("Result OK");
}
else
{
Console.WriteLine("Result error");
}
string FuncModuleString = stri1.ToString() + "; err: " + stri2.ToString();
Console.WriteLine(FuncModuleString);
}
}

I always get "fnFuncModule_GetVersion(): Error at Startup!" message.

In C code everything goes well:

#include "FuncModuleCpp.h"

int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR m_sVersion [kLEN_MAX_STRING + 1];
_TCHAR m_sError [kLEN_MAX_STRING + 1];

if (!fnFuncModule_GetVersion(m_sVersion, m_sError))
// error at service GetVersion
{
// show error
_tprintf(_TEXT("\n"));
_tprintf(_TEXT("########## %s\n"), m_sError);
} else {
_tprintf(_TEXT("\n"));
_tprintf(_TEXT("Version of FuncModule = %s\n"), m_sVersion);
_tprintf(_TEXT("\n"));
}
return 0;
}

If I have a look into the exports, I see following:

C:\Program Files\Microsoft Visual Studio 8\vc\bin>dumpbin/exports d:\FuncModuleCpp.dll
Microsoft (R) COFF/PE Dumper Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file d:\FuncModuleCpp.dll

File Type: DLL

Section contains the following exports for FuncModuleCpp.dll

00000000 characteristics
441AA65E time date stamp Fri Mar 17 13:06:54 2006
0.00 version
1 ordinal base
9 number of functions
9 number of names

ordinal hint RVA name

1 0 00001710 fnFuncModule_AllocDevice
2 1 00001110 fnFuncModule_GetInfoAllDevices
3 2 000023F0 fnFuncModule_GetInfoCapture
4 3 000013F0 fnFuncModule_GetInfoDevice
5 4 00002730 fnFuncModule_GetVersion
6 5 00001A60 fnFuncModule_ReleaseDevice
7 6 00001D70 fnFuncModule_StartCapture
8 7 000020F0 fnFuncModule_StopCapture
9 8 000029D0 fnFuncModule_Test2

Summary

3000 .data
2000 .rdata
2000 .reloc
1000 .rsrc
A000 .text

But if I dump other DLLs, I see some addresses as well - for example:


C:\Program Files\Microsoft Visual Studio 8\vc\bin>dumpbin/exports d:\ClassicC_DLL.dll
Microsoft (R) COFF/PE Dumper Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file d:\ClassicC_DLL.dll

File Type: DLL

Section contains the following exports for ClassicC_DLL.dll

00000000 characteristics
47205FF1 time date stamp Thu Oct 25 11:20:49 2007
0.00 version
1 ordinal base
2 number of functions
2 number of names

ordinal hint RVA name

1 0 000110F0 ExportedFunction = @ILT+235(_ExportedFunction)
2 1 0001117C ExportedSum = @ILT+375(_ExportedSum)

What should I do to get the first DLL to work Maybe this is not the right forum s please can you point me somewhere else as well

Thank you in advance,
Vaclav




Re: Visual C# General DLL error at startup when using old DLL

Mattias Sjogren

VaclavPe wrote:

I always get "fnFuncModule_GetVersion(): Error at Startup!" message.

Can you post the exact exception / error message you get






Re: Visual C# General DLL error at startup when using old DLL

VaclavPe

The output is following:

D:\bin\Debug>CsharpTest.exe
Result OK
; err: fnFuncModule_GetVersion(): Error at Startup!

D:\bin\Debug>

So it looks like the code returns TRUE but Version variable is empty and Error is the string I sent before.

I noticed that the main DLL calls another subsequent DLLs. If I delete them, in classic C it ends with error. But in C# it does not matter. I always get the same result.

Unfortunately I don't have source code from this DLL so I can't say anything about what is inside...






Re: Visual C# General DLL error at startup when using old DLL

VaclavPe

I have one update. I installed newer version of the DLL and now I have following message:

D:\bin\Debug>CsharpTest.exe
Result OK
; err: fnFuncModule_GetVersion(): Error at init of COM library!

I even do not know if this message comes from this DLL or from C# part. The definition of the function is like following:

extern "C" __declspec(dllexport) BOOLEAN fnFuncModule_GetVersion( _TCHAR* sVersionOut, _TCHAR* sErrorOut);

And the C# code I use:

#region Dll Imports

[DllImport("FuncModuleCpp.dll", EntryPoint = "fnFuncModule_GetVersion",

SetLastError = false, CharSet = CharSet.Unicode, ExactSpelling = true,

CallingConvention = CallingConvention.StdCall)]

extern static bool fnFuncModule_GetVersion( StringBuilder sVersionOut, StringBuilder sErrorOut);

#endregion

So maybe the StringBuilder marshalling is wrong How to do it correctly