C# - Problem to receive data from a C++ Dll

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a C# program and want to call a dll, but have problem to receive the
data from the dll.

The dll declaration is the following :
"extern "C" void GetSN(TCHAR** pData)"

The Dll call is the following :
[DllImport("mydll.dll")]
private static extern void GetSN(ref string[] val1);
....
string[] valueStr = new string[6];
....
GetSN(ref valueStr);

I have the following error :
"An unmanaged exception was happened : 'System.NotSupportedException'"

Does anybody have an idea of the problem ?
Thanks in advance for any answer.
 
I'm not too sure about this, but are you sure you can convert a string
array to a single char array?

This is what I see from your code:
"extern "C" void GetSN(TCHAR** pData)" <--takes a pointer to a TCHAR
array

string[] valueStr = new string[6]; <-- a string array
....
GetSN(ref valueStr) <-- you're sending a string array by reference
(pointer to a pointer to a pointer to a character....)

The exception means that the invoked method is not supported.
Basically, it is not recognising your function GetSN.

Like I said, I'm not certain about this. But you can look into your
code and verify this.
 
Hi,

You are probably right but I don't know which type of variable I can use.

I'm asking me something else also : the dll I'm using is a dll which was
created by embedded Visual C++ 3.0 and I want to use it on a Pocket PC 2003
device....
I know that eVC++3.0 is compatible with Pocket PC 2002 but I don't know if
it's compatible with Pocket PC 2003.
 
I think you can use a char type variable and it'll do a conversion by
itself to TCHAR when you call the function. eVC++3.0 should be
compatible with PocketPC2003.

Good Luck.
 
I looked the parameters of the dll. They are the following :

///////////////////////////////////////////////////////
// Function: Retrieve serial number
//
// Parameters:
// 1 [OUT] - Value (6 digits in hex format - 12 characters)
// 2 [OUT] - Result
extern "C" void GetSN(TCHAR** pData)
....
///////////////////////////////////////////////////////

I changed "string[]" by "char[]" in the C# program :
-------------------
[DllImport("mydll.dll")]
private static extern void GetSN(ref char[] val1);
....
char[] valueStr = new char[12];
.....
GetSN(ref valueStr);
MessageBox.Show(valueStr.ToString(), "Info");
.....
Can I do that by that way ?

Thanks in advance for any answer.
 
It should work if you change the following line:

MessageBox.Show(valueStr.ToString(), "Info");

to the following:
MessageBox.Show(new String(valueStr), "Info");

Otherwise, you will get compilation errors. In C#, you cannot convert
a character array to a string by using the .ToString() function. The
way to do it is to create a string item like above.
 
Thanks for your answer but now I have an error :
"System.ArgumentOutOfRangeException" when "GetSN(ref valueStr);" function is
executed.
 
This means that your array is not big enough to receive the data. Are
you sure that the

When you do :
char[] valueStr = new char[12];

try using a larger size (use 100 for testing purposes), and then insert
a breakpoint to see what is the actual size of the returned data.

Good Luck.
 
If I try to do what you said in debug mode, the program locked itself.
In normal mode, the program closes itself when the dll is called.

I don't see where the problem is.
 
Hi,

I followed the document you showed me, but I have when I debug the following
message : System.MissingMethodException.

I don't understand why, now I'm using the following code.

Declaration of the dll in the C # program :

---------------------------------------------------------
[DllImport("Intermec7xxInfo.dll",EntryPoint="GetSN")]
private static extern void GetSN(StringBuilder s);
......
// Call to the dll
StringBuilder valueStr = new StringBuilder("");
....
GetSN(valueStr);
....
-----------------------------------------------------------

And my dll function in C++ is the following.
-------------------
....
extern "C" void GetSN(LPTSTR str)
....
-------------------

Perhaps the problem is that I'm using a program destinated to Pocket PC ?
However, I can download the dll on the device which is on Pocket PC 2003, I
have no error, so I suppose that there is no error with the dll...

If you have any idea...

Thanks in advance.
 
Try this
private static extern void GetSN([MarshalAs(UnmanagedType.LPStr)] String s);


U¿ytkownik "Gandalf said:
Hi,

I followed the document you showed me, but I have when I debug the following
message : System.MissingMethodException.

I don't understand why, now I'm using the following code.

Declaration of the dll in the C # program :

---------------------------------------------------------
[DllImport("Intermec7xxInfo.dll",EntryPoint="GetSN")]
private static extern void GetSN(StringBuilder s);
.....
// Call to the dll
StringBuilder valueStr = new StringBuilder("");
...
GetSN(valueStr);
...
-----------------------------------------------------------

And my dll function in C++ is the following.
-------------------
...
extern "C" void GetSN(LPTSTR str)
...
-------------------

Perhaps the problem is that I'm using a program destinated to Pocket PC ?
However, I can download the dll on the device which is on Pocket PC 2003, I
have no error, so I suppose that there is no error with the dll...

If you have any idea...

Thanks in advance.


Tegdeep said:
That's pretty strange. The problem could be with the way you try to
get the data. See if you find anything on this website :

http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/

It discusses unmanaged DLL's in C# .
Let me know what the solution is, if you find it.
 
Back
Top