DllImport declaring and using methods from C++ in C#










0















I have used DllImport in the past, but I am having a lot of trouble trying to figure out the correct way to declare and use a particular method due to the parameter types. Here is the C++ prototype:



// PTRobot_EnumRobots
//
// Description:
// Function to enumerate the Robots on the system.
// Params:
// phRobots points to an array of HANDLEs to store
// the Robots found.
// pdwNumRobots points to a DWORD containing the number of HANDLEs
// in the phRobots array. This value is an input
// and an output. The user should specify the size
// (number of HANDLEs) of the phRobots array on input.
// The value of the pdwNumRobots on output will be the
// number of robots found.
//
// Notes:
// Both params will be updated upon successful completion of this
// command. phRobots will contain handles to robots connected to
// this system. pdwNumRobots will will be updated with the number of
// robots found.
// Also, note that the hDrives array in the PTRobotInfo will not be
// valid until PTRobot_EnumDrives is called.
//
// Return:
// PTROBOT_OK if Successful
// PTROBOT_INVALID_ROBOT if no robots found
// PTROBOT_SEQUENCE if this command is called out of sequence
// PTROBOT_INTERNAL if an internal error occurred
// PTROBOT_OVERFLOW if the number of robots found is > the value in
// pdwNumRobots
//
///////////////////////////
DWORD WINAPI PTRobot_EnumRobots(HANDLE * phRobots, DWORD * pdwNumRobots);


And here is the C++ usage:



HANDLE hRobots[10];
DWORD dwRet=0, dwRobots=10;

dwRet = PTRobot_EnumRobots(hRobots, &dwRobots);


Here is my attempt at declaring it in C#:



[DllImport("PTRobot.dll", CharSet = CharSet.Unicode)]
private static extern uint PTRobot_EnumRobots(ref IntPtr phRobots, ref uint pdwNumRobots);


usage:



IntPtr robotArray = new IntPtr[1];
UInt32 numRobots = 1;

status = PTRobotDLL.EnumRobots(ref robotArray, ref numRobots);


but I don't think that is right, because I keep getting "INVALID_ROBOT" returned, even though the method return "OK" in C++. I believe the issue is when it is trying to write back to those references, probably because I am not sure how to declare them.



Any insight into the proper way to declare the method in C#? especially with the odd HANDLE array reference?










share|improve this question






















  • Does this also happen with IntPtr robotArray = new IntPtr[10]; UInt32 numRobots = 10; which would more closely correspond to the C++ use?

    – Klaus Gütter
    Nov 14 '18 at 19:56











  • @KlausGütter Yes, no change in the return from playing with the array size or the int value

    – Jonathon Hoaglin
    Nov 14 '18 at 20:00






  • 1





    First param should be IntPtr phRobots

    – David Heffernan
    Nov 14 '18 at 20:37











  • @DavidHeffernan Are you saying it doesn't need the "ref" keyword?

    – Jonathon Hoaglin
    Nov 14 '18 at 22:12






  • 1





    An array is already a reference type, decays to a pointer to the object. To the first element of the array, much like it does in C. So you have to delete ref. The only thing you have to worry about is to make the array large enough, that's why the PTROBOT_OVERFLOW error exists.

    – Hans Passant
    Nov 14 '18 at 23:51
















0















I have used DllImport in the past, but I am having a lot of trouble trying to figure out the correct way to declare and use a particular method due to the parameter types. Here is the C++ prototype:



// PTRobot_EnumRobots
//
// Description:
// Function to enumerate the Robots on the system.
// Params:
// phRobots points to an array of HANDLEs to store
// the Robots found.
// pdwNumRobots points to a DWORD containing the number of HANDLEs
// in the phRobots array. This value is an input
// and an output. The user should specify the size
// (number of HANDLEs) of the phRobots array on input.
// The value of the pdwNumRobots on output will be the
// number of robots found.
//
// Notes:
// Both params will be updated upon successful completion of this
// command. phRobots will contain handles to robots connected to
// this system. pdwNumRobots will will be updated with the number of
// robots found.
// Also, note that the hDrives array in the PTRobotInfo will not be
// valid until PTRobot_EnumDrives is called.
//
// Return:
// PTROBOT_OK if Successful
// PTROBOT_INVALID_ROBOT if no robots found
// PTROBOT_SEQUENCE if this command is called out of sequence
// PTROBOT_INTERNAL if an internal error occurred
// PTROBOT_OVERFLOW if the number of robots found is > the value in
// pdwNumRobots
//
///////////////////////////
DWORD WINAPI PTRobot_EnumRobots(HANDLE * phRobots, DWORD * pdwNumRobots);


And here is the C++ usage:



HANDLE hRobots[10];
DWORD dwRet=0, dwRobots=10;

dwRet = PTRobot_EnumRobots(hRobots, &dwRobots);


Here is my attempt at declaring it in C#:



[DllImport("PTRobot.dll", CharSet = CharSet.Unicode)]
private static extern uint PTRobot_EnumRobots(ref IntPtr phRobots, ref uint pdwNumRobots);


usage:



IntPtr robotArray = new IntPtr[1];
UInt32 numRobots = 1;

status = PTRobotDLL.EnumRobots(ref robotArray, ref numRobots);


but I don't think that is right, because I keep getting "INVALID_ROBOT" returned, even though the method return "OK" in C++. I believe the issue is when it is trying to write back to those references, probably because I am not sure how to declare them.



Any insight into the proper way to declare the method in C#? especially with the odd HANDLE array reference?










share|improve this question






















  • Does this also happen with IntPtr robotArray = new IntPtr[10]; UInt32 numRobots = 10; which would more closely correspond to the C++ use?

    – Klaus Gütter
    Nov 14 '18 at 19:56











  • @KlausGütter Yes, no change in the return from playing with the array size or the int value

    – Jonathon Hoaglin
    Nov 14 '18 at 20:00






  • 1





    First param should be IntPtr phRobots

    – David Heffernan
    Nov 14 '18 at 20:37











  • @DavidHeffernan Are you saying it doesn't need the "ref" keyword?

    – Jonathon Hoaglin
    Nov 14 '18 at 22:12






  • 1





    An array is already a reference type, decays to a pointer to the object. To the first element of the array, much like it does in C. So you have to delete ref. The only thing you have to worry about is to make the array large enough, that's why the PTROBOT_OVERFLOW error exists.

    – Hans Passant
    Nov 14 '18 at 23:51














0












0








0








I have used DllImport in the past, but I am having a lot of trouble trying to figure out the correct way to declare and use a particular method due to the parameter types. Here is the C++ prototype:



// PTRobot_EnumRobots
//
// Description:
// Function to enumerate the Robots on the system.
// Params:
// phRobots points to an array of HANDLEs to store
// the Robots found.
// pdwNumRobots points to a DWORD containing the number of HANDLEs
// in the phRobots array. This value is an input
// and an output. The user should specify the size
// (number of HANDLEs) of the phRobots array on input.
// The value of the pdwNumRobots on output will be the
// number of robots found.
//
// Notes:
// Both params will be updated upon successful completion of this
// command. phRobots will contain handles to robots connected to
// this system. pdwNumRobots will will be updated with the number of
// robots found.
// Also, note that the hDrives array in the PTRobotInfo will not be
// valid until PTRobot_EnumDrives is called.
//
// Return:
// PTROBOT_OK if Successful
// PTROBOT_INVALID_ROBOT if no robots found
// PTROBOT_SEQUENCE if this command is called out of sequence
// PTROBOT_INTERNAL if an internal error occurred
// PTROBOT_OVERFLOW if the number of robots found is > the value in
// pdwNumRobots
//
///////////////////////////
DWORD WINAPI PTRobot_EnumRobots(HANDLE * phRobots, DWORD * pdwNumRobots);


And here is the C++ usage:



HANDLE hRobots[10];
DWORD dwRet=0, dwRobots=10;

dwRet = PTRobot_EnumRobots(hRobots, &dwRobots);


Here is my attempt at declaring it in C#:



[DllImport("PTRobot.dll", CharSet = CharSet.Unicode)]
private static extern uint PTRobot_EnumRobots(ref IntPtr phRobots, ref uint pdwNumRobots);


usage:



IntPtr robotArray = new IntPtr[1];
UInt32 numRobots = 1;

status = PTRobotDLL.EnumRobots(ref robotArray, ref numRobots);


but I don't think that is right, because I keep getting "INVALID_ROBOT" returned, even though the method return "OK" in C++. I believe the issue is when it is trying to write back to those references, probably because I am not sure how to declare them.



Any insight into the proper way to declare the method in C#? especially with the odd HANDLE array reference?










share|improve this question














I have used DllImport in the past, but I am having a lot of trouble trying to figure out the correct way to declare and use a particular method due to the parameter types. Here is the C++ prototype:



// PTRobot_EnumRobots
//
// Description:
// Function to enumerate the Robots on the system.
// Params:
// phRobots points to an array of HANDLEs to store
// the Robots found.
// pdwNumRobots points to a DWORD containing the number of HANDLEs
// in the phRobots array. This value is an input
// and an output. The user should specify the size
// (number of HANDLEs) of the phRobots array on input.
// The value of the pdwNumRobots on output will be the
// number of robots found.
//
// Notes:
// Both params will be updated upon successful completion of this
// command. phRobots will contain handles to robots connected to
// this system. pdwNumRobots will will be updated with the number of
// robots found.
// Also, note that the hDrives array in the PTRobotInfo will not be
// valid until PTRobot_EnumDrives is called.
//
// Return:
// PTROBOT_OK if Successful
// PTROBOT_INVALID_ROBOT if no robots found
// PTROBOT_SEQUENCE if this command is called out of sequence
// PTROBOT_INTERNAL if an internal error occurred
// PTROBOT_OVERFLOW if the number of robots found is > the value in
// pdwNumRobots
//
///////////////////////////
DWORD WINAPI PTRobot_EnumRobots(HANDLE * phRobots, DWORD * pdwNumRobots);


And here is the C++ usage:



HANDLE hRobots[10];
DWORD dwRet=0, dwRobots=10;

dwRet = PTRobot_EnumRobots(hRobots, &dwRobots);


Here is my attempt at declaring it in C#:



[DllImport("PTRobot.dll", CharSet = CharSet.Unicode)]
private static extern uint PTRobot_EnumRobots(ref IntPtr phRobots, ref uint pdwNumRobots);


usage:



IntPtr robotArray = new IntPtr[1];
UInt32 numRobots = 1;

status = PTRobotDLL.EnumRobots(ref robotArray, ref numRobots);


but I don't think that is right, because I keep getting "INVALID_ROBOT" returned, even though the method return "OK" in C++. I believe the issue is when it is trying to write back to those references, probably because I am not sure how to declare them.



Any insight into the proper way to declare the method in C#? especially with the odd HANDLE array reference?







c# c++ dllimport






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 19:51









Jonathon HoaglinJonathon Hoaglin

579




579












  • Does this also happen with IntPtr robotArray = new IntPtr[10]; UInt32 numRobots = 10; which would more closely correspond to the C++ use?

    – Klaus Gütter
    Nov 14 '18 at 19:56











  • @KlausGütter Yes, no change in the return from playing with the array size or the int value

    – Jonathon Hoaglin
    Nov 14 '18 at 20:00






  • 1





    First param should be IntPtr phRobots

    – David Heffernan
    Nov 14 '18 at 20:37











  • @DavidHeffernan Are you saying it doesn't need the "ref" keyword?

    – Jonathon Hoaglin
    Nov 14 '18 at 22:12






  • 1





    An array is already a reference type, decays to a pointer to the object. To the first element of the array, much like it does in C. So you have to delete ref. The only thing you have to worry about is to make the array large enough, that's why the PTROBOT_OVERFLOW error exists.

    – Hans Passant
    Nov 14 '18 at 23:51


















  • Does this also happen with IntPtr robotArray = new IntPtr[10]; UInt32 numRobots = 10; which would more closely correspond to the C++ use?

    – Klaus Gütter
    Nov 14 '18 at 19:56











  • @KlausGütter Yes, no change in the return from playing with the array size or the int value

    – Jonathon Hoaglin
    Nov 14 '18 at 20:00






  • 1





    First param should be IntPtr phRobots

    – David Heffernan
    Nov 14 '18 at 20:37











  • @DavidHeffernan Are you saying it doesn't need the "ref" keyword?

    – Jonathon Hoaglin
    Nov 14 '18 at 22:12






  • 1





    An array is already a reference type, decays to a pointer to the object. To the first element of the array, much like it does in C. So you have to delete ref. The only thing you have to worry about is to make the array large enough, that's why the PTROBOT_OVERFLOW error exists.

    – Hans Passant
    Nov 14 '18 at 23:51

















Does this also happen with IntPtr robotArray = new IntPtr[10]; UInt32 numRobots = 10; which would more closely correspond to the C++ use?

– Klaus Gütter
Nov 14 '18 at 19:56





Does this also happen with IntPtr robotArray = new IntPtr[10]; UInt32 numRobots = 10; which would more closely correspond to the C++ use?

– Klaus Gütter
Nov 14 '18 at 19:56













@KlausGütter Yes, no change in the return from playing with the array size or the int value

– Jonathon Hoaglin
Nov 14 '18 at 20:00





@KlausGütter Yes, no change in the return from playing with the array size or the int value

– Jonathon Hoaglin
Nov 14 '18 at 20:00




1




1





First param should be IntPtr phRobots

– David Heffernan
Nov 14 '18 at 20:37





First param should be IntPtr phRobots

– David Heffernan
Nov 14 '18 at 20:37













@DavidHeffernan Are you saying it doesn't need the "ref" keyword?

– Jonathon Hoaglin
Nov 14 '18 at 22:12





@DavidHeffernan Are you saying it doesn't need the "ref" keyword?

– Jonathon Hoaglin
Nov 14 '18 at 22:12




1




1





An array is already a reference type, decays to a pointer to the object. To the first element of the array, much like it does in C. So you have to delete ref. The only thing you have to worry about is to make the array large enough, that's why the PTROBOT_OVERFLOW error exists.

– Hans Passant
Nov 14 '18 at 23:51






An array is already a reference type, decays to a pointer to the object. To the first element of the array, much like it does in C. So you have to delete ref. The only thing you have to worry about is to make the array large enough, that's why the PTROBOT_OVERFLOW error exists.

– Hans Passant
Nov 14 '18 at 23:51













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307804%2fdllimport-declaring-and-using-methods-from-c-in-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307804%2fdllimport-declaring-and-using-methods-from-c-in-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo