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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:35:33+00:00 2026-06-16T05:35:33+00:00

Possible Duplicate: Passing char pointer from C# to c++ function I have this type

  • 0

Possible Duplicate:
Passing char pointer from C# to c++ function

I have this type of problem:

I have a c++ function with this signature:

int myfunction ( char* Buffer, int * rotation)

The buffer parameter must be filled with space chars ( 0x20 hex )

In C++ i can solve the problem simply doing this:

char* buffer = (char *)malloc(256);
memset(buffer,0x20,256);
res = myfunction (buffer, rotation);

I’m trying to call this function from C#.

This is my p/invoke declaration:

[DllImport("mydll.dll", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern unsafe int myfunction (StringBuilder Buffer, int* RotDegree);

In my C# class i’ve tried to do this:

StringBuilder buffer = new StringBuilder(256);
buffer.Append(' ', 256);
...
myfunction(buffer, rotation);

but it doesn’t work….

Anyone can help me?

Thanks.

  • 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-16T05:35:34+00:00Added an answer on June 16, 2026 at 5:35 am

    Your p/invoke doesn’t look quite right. It should (presumably) use the Cdecl calling convention. You should not use SetLastError. And there’s no need for unsafe code.

    I would write it like this:

    [DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
    private static extern int myfunction(StringBuilder Buffer, ref int RotDegree);
    

    Then call it like this:

    StringBuilder buffer = new StringBuilder(new String(' ', 256));
    int rotation = ...;
    int retVal = myfunction(buffer, ref rotation);
    

    I didn’t specify the CharSet since Ansi is the default.

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

Sidebar

Related Questions

Possible Duplicate: Splitting a string in C++ I have this vector function from one
Possible Duplicate: passing a pointer by reference in c++ I have a function that
Possible Duplicate: C++ passing variables in from one Function to the Next. The Program
Possible Duplicate: passing variables from php to javascript I am using this awesome jQuery
Possible Duplicate: Passing arguments to a console application from C# I have a console
Possible Duplicate: passing index from for loop to ajax callback function (javascript) I've been
Possible Duplicate: jquery passing $(this) to other functions I am writing a Javascript/jQuery function
Possible Duplicate: C/C++: Passing variable number of arguments around Imagine I have a function
Possible Duplicate: Passing a Int value to another class I have been trying to
Possible Duplicate: Difference between passing array and array pointer into function in C I

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.