Solved

C# Class subroutines, How to pass the eqivilant of VB's byRef

Posted on 2010-09-24
4
553 Views
Last Modified: 2013-12-16
I have some C subroutines in DLL's that I want to convert to C#. They basically have parms:

Input buffer (bytes)
Input length (int32)
Outbut buffer (bytes)
Output length (int32)

In VB.net, we simply define a subroutine with parameters defined as byRef, for example:

Sub MySub(..., byRef OutBuff() as Byte, byRef outLen as Int32)
        ' Do something
End Sub

This passes the data's pointers to the subroutine (and the length attribute as part of the byte array object). Thus the subroutine does not have any size limit/wasted memory tradeoffs. How do I do this in C#? In my attempts I keep getting "unsafe code", which VB will not allow me to call.

Thank you.
0
Comment
Question by:MikeBroderick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Umar Topia
ID: 33754221
function MySub(ref bytes[] byInput)
{}
0
 
LVL 10

Accepted Solution

by:
Umar Topia earned 500 total points
ID: 33754229
Sry abt the typo:

void MySub(ref bytes[] byInput)
{}
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 33754303
Hi MikeBroderick;

When calling the method MySub in umartopia example you also need to have the ref key word.

// Calling MySub method:
MySub ( ... ref myByteArray, ... );

Fernando
0
 

Author Closing Comment

by:MikeBroderick
ID: 33754339
Thanks. I'm putting it in now.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Stop Git from being my repository 1 44
Web page design problem 3 37
.NET Redemption/Outlook Examples 3 24
Intermittent OleDbConnection Error 20 43
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question