Step-by-Step Guide: Embedding DWG Viewer OCX in .NET and VB6 Projects

Step-by-Step Guide: Embedding DWG Viewer OCX in .NET and VB6 Projects

Overview

This guide shows how to register, add, and use a DWG Viewer OCX ActiveX control in both .NET (WinForms) and VB6 projects, plus common troubleshooting and deployment tips.

Prerequisites

  • Windows development machine (Windows 10+).
  • DWG Viewer OCX file (e.g., Dwgvw.ocx) and license if required.
  • Administrator rights for OCX registration.
  • Visual Studio (for .NET) and Visual Basic 6 IDE (for VB6).
  • If targeting 64-bit Windows, plan to run 32-bit host process if the OCX is 32-bit.

Part A — Register the OCX

  1. Copy the OCX file to a stable folder (e.g., C:\Program Files\DWGViewer).
  2. Open an elevated Command Prompt.
  3. Register the OCX:
    • For 32-bit OCX on 64-bit Windows:

      Code

      C:\Windows\SysWOW64\regsvr32 “C:\Program Files\DWGViewer\Dwgvw.ocx”
    • For 32-bit on 32-bit or 64-bit OCX on 64-bit:

      Code

      regsvr32 “C:\Program Files\DWGViewer\Dwgvw.ocx”
  4. Verify success message. If registration fails, ensure dependencies (Visual C++ runtimes) are installed.

Part B — Embedding in VB6

  1. Open VB6 IDE and your project (or create a new Standard EXE).
  2. Add control to Toolbox: Project → Components → Browse → select Dwgvw.ocx. Check the control and click OK.
  3. Drag the DWG Viewer control from the toolbox onto a form. It will create an instance (e.g., AxDwgViewer1).
  4. Basic usage (form code):

    Code

    Private Sub Form_Load() AxDwgViewer1.LoadFile “C:\Drawings\example.dwg”

    AxDwgViewer1.ZoomToFit 

    End Sub

  5. Handle events exposed by the OCX via the control’s event list in code.
  6. Build and test in the VB6 IDE; remember the final EXE must be distributed with the OCX and correct registration.

Part C — Embedding in .NET (WinForms)

Note: .NET WinForms hosts COM/ActiveX via Windows Forms ActiveX interop.

  1. Create/Open a WinForms project in Visual Studio (target x86 if OCX is 32-bit).
  2. Add the OCX to the Toolbox:
    • Right-click Toolbox → Choose Items → COM Components tab → find the DWG Viewer control → check it → OK.
  3. Drag the control onto a form; Visual Studio generates an interop assembly (AxInterop.Dwgvw.dll) and a wrapper.
  4. Basic usage (C#):

    csharp

    using System; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { axDwgViewer1.LoadFile(@“C:\Drawings\example.dwg”); axDwgViewer1.ZoomToFit(); } }
  5. Set project Platform Target to x86 (Project Properties → Build).
  6. Build and run. If Visual Studio cannot find the control, ensure the OCX is registered and restart Visual Studio.

Part D — Common Properties & Methods (typical)

  • LoadFile(path) — load a DWG/DXF.
  • ZoomToFit() / ZoomIn() / ZoomOut() — view controls.
  • Print() / ExportImage(path) — output options.
  • PanStart/End or SetView(x,y,scale) — navigation.
    (Refer to OCX documentation for exact API names.)

Part E — Events to Watch

  • OnFileLoaded / FileOpenError — load status.
  • OnMouseClick / OnDoubleClick — user interactions.
  • OnZoomChanged / OnViewChanged — view updates.

Part F — Deployment

  1. Include OCX and any dependent DLLs in installer.
  2. Use regsvr32 during installation (elevated) or use a proper installer (MSI) that registers COM components.
  3. If deploying on 64-bit Windows and OCX is 32-bit, ensure host app is 32-bit.
  4. Install required VC++ runtimes.
  5. Test on clean VMs matching target OS versions.

Troubleshooting

  • “Class not registered” — OCX not registered or wrong bitness.
  • “Interface not registered” — missing dependencies; run Dependency Walker or similar.
  • Runtime errors in .NET — check interop assembly generation; try manually generating with aximp.exe.
  • Licensing errors — ensure license info stored in registry or provided per vendor docs.

Security & Stability Tips

  • Run the control in a least-privileged process.
  • Validate and sanitize any file paths before loading.
  • Keep OCX updated to patch vulnerabilities.

References & Further Steps

  • Consult the vendor’s OCX API documentation for exact method/event names.
  • Use aximp.exe and tlbexp.exe if custom interop assembly control is needed.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *