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 8264205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:22:22+00:00 2026-06-08T04:22:22+00:00

I have a struct in C file as shown below struct Parameter{ char param1[20];

  • 0

I have a struct in C file as shown below

struct Parameter{
    char param1[20];
    char param2[20];
}

and also a function in C file which takes this struct as parameter along with char* as another parameter as shown below

extern "C" __declspec(dllexport) void GetValue(char* opt,struct Parameter param);
void GetValue(char* opt, struct Parameter params)
{
printf("%s", params->param1);
}

I want to call it from my C# application using marshalling. I have created a similar struct in C#

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class Parameters
{      
    public string Param1 { get; set; }     
    public string Param1 { get; set; }   
}

and calling it in C# using the below method

 [DllImport(@"C:\Test\CTestDll.dll",CallingConvention = CallingConvention.Cdecl,CharSet=CharSet.Ansi)]      
 public static extern void GetValue([MarshalAs(UnmanagedType.LPStr)]StringBuilder sbOut, [MarshalAs(UnmanagedType.LPStruct)]Parameters sbIn);

but the result which is a print statement is printing null.I am not very good in C programming. Kindly help me to sort it out. Where I am wrong, Is it in the C function or marshalling from C#

  • 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-08T04:22:24+00:00Added an answer on June 8, 2026 at 4:22 am

    The difference is that in C++ your structure contains raw chars, while in C# you class contains reference to string. MarshalAs attribute will use a char array instead of a reference to string:

    unsafe static class Program
    {
    
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct Parameters
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
        public String Param1;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
        public String Param2;
    }
    
    
    [DllImport(@"CTestDll2.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public static extern void GetValue(StringBuilder sbOut, Parameters sbIn);
    
    
    static void Main(string[] args)
    {
        var p = new Parameters
        {
            Param1 = "abc",
            Param2 = "dfc"
        };
    
        var s = new StringBuilder("some text");
    
        GetValue(s, p);
    }
    

    }

    C++:

    // CTestDll.h
    
    #pragma once
    
    #include <stdio.h>
    
    extern "C" __declspec(dllexport) void GetValue(char* opt, struct Parameter param);
    
    struct Parameter{
        char param1[20];
        char param2[20];
    };
    
    void GetValue(char* opt, struct Parameter params)
    {
        printf("param1: %s, param2: %s, string: %s", params.param1, params.param2, opt);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have CSV file which has timestamp in the first column as shown below
I have to write an object in to binary file.My struct looks like this.
In one file I have this: #include <stdio.h> #include <stdlib.h> static struct node* mynode;
I currently have a library-like .c file (that'll be shown below). I have 2
I have a header file including a structure like this: typedef struct { int
I have C header file containing the following type definition: // example.h typedef struct
i have a file, filedata.mat, containing a 1x1 struct with sub-levels that contain data
I'm saving a struct into a .dat file. Lets suppose I have to edit
Imagine you have a file a.h #include <iostream> template<typename T> struct A{ int magic;
Ok so I have struct like this typedef struct { float x; float y;

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.