Char Pointer from C++ DLL | Newbie | | Join Date: Oct 2009
Posts: 5
| |
I recently converted my application into C# as VS2008 does not support new Data Sources (?!), but now I am stuck on one command from the old DLL.
Here is the old function declaration and command that worked in C++: - typedef short(*pfunc2)(short address, LPSTR cmd, short irqnum);
-
pfunc2 pfSendAT6400Block = (pfunc2)GetProcAddress(hndl, "SendAT6400Block");
-
-
char *testCommands[258] = {"A100:V50:D254000:GO1:"}; //Pointer to a null-terminated string
-
SetTimeout(10000);
-
SendAT6400Block(768, *testCommands, 0);
I definitely have the DLL loaded correctly in C# as the other commands work, but I have a feeling my syntax with sending the commands, which would be the LPSTR cmd delcaration. My C# code snippets are below. - [DllImport("C:\\WINDOWS\\system32\\nt6400.dll", CharSet = CharSet.Auto)]
-
public static extern short SendAT6400Block(short address, [MarshalAs(UnmanagedType.LPStr)] string cmd, short irqnum);
-
-
string commandSTR = "A100:V50:D254000:G01:";
-
NT6400.SetTimeout(2000);
-
int SendBlockReturn = NT6400.SendAT6400Block(768, commandSTR, 0);
So now my SendAT6400Block is not working in C# when it was working in my old C++ code. Could anyone provide any insight or do you need some more information?
Your help, as always, is greatly appreciated.
| |
best answer - posted by vkbishnoi | Quote:
Originally Posted by vkbishnoi try this -
[DllImport("C:\\WINDOWS\\system32\\nt6400.dll", CharSet = CharSet.Ansi)]
-
public static extern short SendAT6400Block(short address, string cmd, short irqnum);
I tried creating a VC++/MFC dll with a function signature
short SendAT6400Block(short a, LPSTR command, short b);
and using the above pinvoke signature i was able to pass the string successfully to this DLL.
Also i tried these signatures in the VC++/MFC dll
short SendAT6400Block(short a, char* command, short b);
short SendAT6400Block(short a, char command[258], short b);
and all of them are working fine. Please note that this VC++/MFC dll is developed using VS2005 as i do not have VC6. In case my previous suggestion is not working then can you share the C++ side function signature.
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: Char Pointer from C++ DLL
Have you tried ensuring that your strings end in a null byte? I don't think .NET strings specifically do.
| | Newbie | | Join Date: Oct 2009
Posts: 5
| | | re: Char Pointer from C++ DLL Quote:
Originally Posted by Plater Have you tried ensuring that your strings end in a null byte? I don't think .NET strings specifically do. So would I just add a \0 to the end of my string to null-terminate it?
If its that easy that would be awesome.
Thanks in advance.
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: Char Pointer from C++ DLL
Give it a try? I'm not sure it's the solution, but it would make a simple fix if it were
| | Newbie | | Join Date: Oct 2009
Posts: 5
| | | re: Char Pointer from C++ DLL
Well the function returned a value of 22 (bytes sent to the controller), but I still got no motion from the controller.
BTW when I send the command its supposed to make the arm attached to the controller move a certain distance with specified acceleration and speed.
My thought is that the command was actually sent and received by the controller (all 22 bytes of it), but the pointer to the null terminated string is not being sent.
Here is the code as of right now: - string commandSTR = "A100:V50:D254000:G01:\0";
-
//IntPtr commandPTR = Marshal.StringToHGlobalAuto(commandSTR);
-
-
NT6400.SetTimeout(2000);
-
int SendBlockReturn = NT6400.SendAT6400Block(768, commandSTR, 0);
-
From reading other stuff online I was also looking at trying an IntPtr to send the string (as the commented line above indicates). Not sure if my code was correct or not, but the string is definitely null terminated and I am not seeing a response.
Anyone have any insight or idea of what might be happening?
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: Char Pointer from C++ DLL
Is there anyway you can see what the arm controller is actually getting?
| | Newbie | | Join Date: Oct 2009
Posts: 5
| | | re: Char Pointer from C++ DLL
The return value of the function is the number of bytes received from the product. I put that value into a messageBox and it displays "22 bytes received" so I think it MAY be working although I am still getting no movement.
Here is the exact text from the function:
"cmd" - Pointer to a null-terminated string that contains on or more commands. Commands must be separateed with a carriage return of colon. The buffer that cmd points to should be 258 bytes in size.
My next attempt (if necessary) is this: - byte[] byteArray;
-
byteArray = new byte[257];
-
-
string commandSTR = "A100:V50:D254000:G01:\0";
-
-
byteArray = StrToByteArray(commandSTR);
-
-
NT6400.SetTimeout(2000);
-
int SendBlockReturn = NT6400.SendAT6400Block(768, byteArray, 0);
Thanks again for your help.
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: Char Pointer from C++ DLL
Hmm, maybe it expects 258bytes?
Try creating your command to be 258bytes long(for safety sake, make all the additional bytes AFTER your command be the \0)
| | Newbie | | Join Date: Oct 2009 Location: Pune, India
Posts: 6
| | | re: Char Pointer from C++ DLL
try this
[DllImport("C:\\WINDOWS\\system32\\nt6400.dll", CharSet = CharSet.Ansi)]
public static extern short SendAT6400Block(short address, string cmd, short irqnum);
| | Newbie | | Join Date: Oct 2009
Posts: 5
| | | re: Char Pointer from C++ DLL Quote:
Originally Posted by vkbishnoi try this
[DllImport("C:\\WINDOWS\\system32\\nt6400.dll", CharSet = CharSet.Ansi)]
public static extern short SendAT6400Block(short address, string cmd, short irqnum); Okay will give that a shot today.
Just one question relating to it though -- why doesn't Auto just automatically use Ansi then?
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | re: Char Pointer from C++ DLL Quote:
Originally Posted by mdonders Okay will give that a shot today.
Just one question relating to it though -- why doesn't Auto just automatically use Ansi then? Probably defaults to unicode (as it should)
| | Newbie | | Join Date: Oct 2009 Location: Pune, India
Posts: 6
| | | re: Char Pointer from C++ DLL Quote:
Originally Posted by vkbishnoi try this -
[DllImport("C:\\WINDOWS\\system32\\nt6400.dll", CharSet = CharSet.Ansi)]
-
public static extern short SendAT6400Block(short address, string cmd, short irqnum);
I tried creating a VC++/MFC dll with a function signature
short SendAT6400Block(short a, LPSTR command, short b);
and using the above pinvoke signature i was able to pass the string successfully to this DLL.
Also i tried these signatures in the VC++/MFC dll
short SendAT6400Block(short a, char* command, short b);
short SendAT6400Block(short a, char command[258], short b);
and all of them are working fine. Please note that this VC++/MFC dll is developed using VS2005 as i do not have VC6. In case my previous suggestion is not working then can you share the C++ side function signature.
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|