Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:20:26+00:00 2026-06-12T15:20:26+00:00

I need to get Data from a C DLL to a C# Application. Here

  • 0

I need to get Data from a C DLL to a C# Application. Here is the error :

Cannot marshal field ‘Counters’ of type ‘KnittingWago.Common.WAGO_DATA_TO_USER_T’: Invalid managed/unmanaged type combination (Array fields must be paired with ByValArray or SafeArray).

Here is the DLL .h

#define WAGO_NB_COUNTERS_C 80
#define WAGO_NB_ENCODERS_C 10 

struct WAGO_DATA_TO_USER_T
{
   unsigned int Counters[WAGO_NB_COUNTERS_C];
   int Encoders[WAGO_NB_ENCODERS_C];
   unsigned int Weight;
   bool CalibrationValid;
   bool LastCalibrationFailed;
};

Here is the C# Struct declaration :

const int WAGO_NB_COUNTERS_C = 80;
const int WAGO_NB_ENCODERS_C = 10;

struct WAGO_DATA_TO_USER_T
{
   [MarshalAs(UnmanagedType.U4, SizeConst = GlobalConstant.WAGO_NB_COUNTERS_C)]
   UInt32[] Counters;
   [MarshalAs(UnmanagedType.I4, SizeConst = GlobalConstant.WAGO_NB_ENCODERS_C)]
   Int32[] Encoders;
   UInt32 Weight;
   Boolean CalibrationValid;
   Boolean LastCalibrationFailed;
};

How do I declare the C# struct to get the rigth data without error ?

Thanks


EDIT :

I’ve rewrite the Struct as :

internal struct WAGO_DATA_TO_USER_T
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = GlobalConstant.WAGO_NB_COUNTERS_C)]
    UInt32[] Counters;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = GlobalConstant.WAGO_NB_ENCODERS_C)]
    Int32[] Encoders;
    UInt32 Weight;
    Boolean CalibrationValid;
    Boolean LastCalibrationFailed;
};

No more Error, but the Values are not as expected.

Should be like :

Counter[0] = ~ 5000
Counter[1] = ~ 30000
Counter[2-79] = 0

Really are :

Counter[0] = 40
Counter[1] = 1080228
Counter[2] = 82964616
Counter[3-79] = All have a non-zero value
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:20:28+00:00Added an answer on June 12, 2026 at 3:20 pm

    The Marshalling was correct but the Device I was calling was not initialized by the external unmanaged dll. So I was reading uninitialisez memory that lead to that unvalid data

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get data from xml file <?xml version=1.0?> <xml> <User> <Agent> <id>cr4523</id>
I need to get data from several tables, so I used a query with
I need to get data from flat files to DB using SSIS . The
I am working on a C# project where I need to get data from
I need to get some data from a column. For example I have data
I have 3 tables, I need advice on how to get data from them.
I need a method to get the data from an external editor. def _get_content():
I have huge amount of data from database, i need to get or store
I need to get lots of data (file select , text, date ,...) from
I need to take the data I get from an element id pulled from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.