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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:00:37+00:00 2026-05-26T03:00:37+00:00

I have SAP RPC OCX control that I’d like to use. In C# 4

  • 0

I have SAP RPC OCX control that I’d like to use.
In C# 4 following code works fine:

System.Type t = System.Type.GetTypeFromProgID("SAP.Functions", true);            
dynamic fc = System.Activator.CreateInstance(t, false);
dynamic connection = fc.Connection;
connection.System = "";

Following code does NOT work (even though connection is NOT null)

System.Type t = System.Type.GetTypeFromProgID("SAP.Functions", true);            
dynamic fc = System.Activator.CreateInstance(t, false);
var connection = fc.Connection as SAPLogonCtrl.Connection
connection.System = "";

Following error is thrown:
“Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”

The most bizarre fact is this though:

System.Type t = System.Type.GetTypeFromProgID("SAP.Functions", true);            
dynamic fc = System.Activator.CreateInstance(t, false);
dynamic c1 = fc.Connection;
var c2 = fc.Connection as SAPLogonCtrl.Connection;
if (c1 == c2)
  c2.System = "";

Last line is executed and throws the same exception!!!
Replace c2 with c1 works as expected…

I feel I am missing something trivial and yet I am at a complete loss…
Please help?

Additional info:
Changing from:

dynamic fc = System.Activator.CreateInstance(t, false);

to:

var fc = System.Activator.CreateInstance(t, false) as SAPFunctionsOCX.SAPFunctions;

Makes no difference. c1 still works and c2 still does not.

Additional info #2:
Changing properties on FC itself also works in both cases.

  • 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-26T03:00:38+00:00Added an answer on May 26, 2026 at 3:00 am

    Although it looks like you’re doing the same thing both times, it’s actually completely different: In the first code example:

    dynamic connection = fc.Connection;
    connection.System = "";
    

    what happens is you get fc.Connection, and then invoke the System = property setter using dynamic. The dynamic language runtime goes off and queries the COM interfaces, and invokes a particular method on a particular COM interface. You can’t actually see what interface or method it’s using from the C# side of things.

    In the second example (I’ve replaced the var to make things more clear):

    SAPLogonCtrl.Connection connection = fc.Connection as SAPLogonCtrl.Connection
    connection.System = "";
    

    what happens is you get fc.Connection, and then cast it to SAPLogonCtrl.Connection. You then try and call SAPLogonCtrl.Connection.System = and then it fails.

    I suspect that it’s failing because the object is not actually an instance of SAPLogonCtrl.Connection but it may be a proxy or some other object.

    Most COM interop is split into an interface (there’s most likely an SAPLogonCtrl.IConnection) and a class. When calling methods, you usually need to call through the interface. The dynamic code will be doing it all for you behind the scenes.

    You could try searching for the interface, and calling using that. If it ends up that SAPLogonCtrl.IConnection exists, the solution may be as follows.

    var connection = fc.Connection as SAPLogonCtrl.IConnection // note the I!
    connection.System = "";
    

    At any rate, remember to call through the interface when dealing with COM interop

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

Sidebar

Related Questions

I have this stored procedure that I want to use for my SAP crystal
We have one SAP system in the US (let's call it TKIJVPL1 ), this
We have upgraded our development SAP system from ECC6 SPS3 to ECC6 SPS5 .
I have a C# application that communicates with an SAP function module via a
I have 2 dictionaries that contain employee information form 2 DB tables in SAP
I have to pull some data from an ERP system (SAP) in C#. Without
We have upgraded our development SAP system from (ECC6 and SAP Portal) from SPS3
I have an old Delphi application that uses the SAP ActiveX SAPFunctions var TmpSAPFunctions:
I have very little SAP ERP knowledge. I wrote some code to create a
I have a solution file that is built and deployed with the following MSBuild

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.