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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:44:28+00:00 2026-05-13T21:44:28+00:00

I am referencing a DLL in my C# project as follows: [DllImport(FeeCalculation.dll, CallingConvention =

  • 0

I am referencing a DLL in my C# project as follows:

[DllImport("FeeCalculation.dll", CallingConvention = CallingConvention.StdCall,
           CharSet = CharSet.Ansi)]

        public static extern void FeeCalculation(string cin, string cout, string flimit,
            string frate, string fwindow, string fincrement, string fbird, 
            string fparameter, string fvalidation, string fcoupon);

The FeeCalculation function is exported as follows in the DLL:

extern "C" __declspec(dllexport) void __stdcall FeeCalculation(char *cin, 
char *cout, char *flimit, char *frate,
char *fwindow, char *fincrement, char *fbird,
char *fparameter, char *fvalidation, char *fcoupon);

The DLL function returns a reference to it’s internal structures in the form of char * so if you were to reference this DLL in C++, you would do the following to do the calculation and get the returned structures:

FeeCalculation(buff, (char *)&fans, (char *)fl, (char *)ft, (char *)fw, (char *)fi, (char *)fe, (char *)&fm, (char *)val, (char *)cpn);

Now, how do I retrieve those values that are returned by reference using C#? Meaning, how do I do the same thing in C# to get the returned structures to get my returned calculation? I know I need to create an unsafe method, but I am unclear on how to deal with the memory addresses in C# like you would in C++.

Edit: Below states to use IntPtr but how do you place into identical structure so the fields of the structure can be referenced?

Edit: Here is the returned structure that I am interested in (cout):

struct feeAnswer {


    unsigned int    fee;

    unsigned int    tax1;

    unsigned int    tax2;

    unsigned int    tax3;

    unsigned int    tax4;

    unsigned int    surcharge1;

    unsigned int    surcharge2;

    unsigned int    validationFee;

    unsigned int    couponFee1;

    unsigned int    couponFee2;

    unsigned int    couponFee3;

    unsigned int    couponFee4;

    unsigned short int dstay;       //Day Stay

    unsigned short int mstay;       //Minute Stay

};

Here is the (cin) that I would pass along with other structures (they are zero byte at the moment, I want to get this to work first then I will implement the rest):

struct feeRequest {

    unsigned char   day;

    unsigned char   month;

    unsigned int    year;   //2000 ~ 2099



    unsigned char   hour;

    unsigned char   minute;

    unsigned char   rate;

    unsigned char   validation;



    unsigned char   coupon1;

    unsigned char   coupon2;

    unsigned char   coupon3;

    unsigned char   coupon4;

};
  • 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-05-13T21:44:28+00:00Added an answer on May 13, 2026 at 9:44 pm

    Edit: now that we have structures to work with, a better solution is possible. Just declare structs in C# that match your C++ structs, and use them in the extern declaration

    [StructLayout(LayoutKind.Sequential)]
    public struct feeAnswer {
       public uint    fee;
       public uint    tax1;
       public uint    tax2;
       public uint    tax3;
       public uint    tax4;
       public uint    surcharge1;
       public uint    surcharge2;
       public uint    validationFee;
       public uint    couponFee1;
       public uint    couponFee2;
       public uint    couponFee3;
       public uint    couponFee4;
       public ushort  dstay;       //Day Stay
       public ushort  mstay;       //Minute Stay
       };
    
      [StructLayout(LayoutKind.Sequential, Pack=1)]
      public struct feeRequest {
       public byte   day;
       public byte   month;
       public uint   year;   //2000 ~ 2099
       public byte   hour;
       public byte   minute;
       public byte   rate;
       public byte   validation;
       public byte   coupon1;
       public byte   coupon2;
       public byte   coupon3;
       public byte   coupon4;
       };
    
      [DllImport ("FeeCalculation.dll", CallingConvention = CallingConvention.StdCall,
                 CharSet = CharSet.Ansi)]
      public static extern void FeeCalculation (
              feeRequest cin,
              out feeAnswer cout,
               ...
    
    
    
            ....
    

    original answer (before we had structs) below


    It appears to me that these are not references to internal strings, but rather pointers to string buffers that will be filled in by the call. If you were returning string pointers, then these would be declared char** rather than char*.

    So I think these are just standard out parameters. There’s just a lot of them. So your C# interop would look like this

    [DllImport("FeeCalculation.dll", CallingConvention = CallingConvention.StdCall,
               CharSet = CharSet.Ansi)]
    public static extern void FeeCalculation(string cin, 
            [MarshalAs(UnmanagedType.LPStr, SizeConst=100)]
            out string cout, 
            [MarshalAs(UnmanagedType.LPStr, SizeConst=100)]
            out string flimit,
    

    or this if your “strings” aren’t really strings

    [DllImport("FeeCalculation.dll", CallingConvention = CallingConvention.StdCall,
               CharSet = CharSet.Ansi)]
    public static extern void FeeCalculation(string cin, 
            [MarshalAs(UnmanagedType.LPArray, SizeConst=100)]
            out byte[] cout, 
            [MarshalAs(UnmanagedType.LPArray, SizeConst=100)]
            out byte[] flimit,
            ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have a class library project name Utilities.dll as follows: public static class
I'm referencing a static library . I've dragged the library project into my app
I am referencing OpenNETCF.Desktop.Communication (Rapi.dll) in my project and have the following problem: When
When working on a VS2005 project that involves referencing Microsoft.SharePoint.dll, building the project causes
A Windows Phone application referencing a dll(another class library project). There is an asynchronous
Is there any way, other than referencing the Microsoft.VisualBasic.dll (such as as below in
I am referencing a COM structure that starts as follows: [scriptable, uuid(ae9e84b5-3e2d-457e-8fcd-5bbd2a8b832e)] interface nsICacheSession
In a project using .Net4 - should there be any issues referencing dlls built
I wish to include the dll output by a project built from an external
I am using VS2010 and I have a C++ project that is referencing and

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.