Explainer
Why a VB ActiveX DLL Does Not Work From WinCC C-Scripts
Short answer
A Visual Basic ActiveX DLL fails in a WinCC C-script because it is a COM server, not a flat Win32 DLL: the script binds only exported function names, does no COM initialization, and the class must be registered on the runtime PC. Register the DLL with regsvr32 and call it through a small C wrapper that handles the COM lifecycle.
On this page
A Visual Basic ActiveX DLL does not work from a WinCC C-script because it is not the kind of DLL the script can load: it is a COM server. The script engine is plain ANSI C that binds declared names against a DLL's export table, while a VB ActiveX DLL publishes classes through the registry and hands out its methods only through COM interfaces. Siemens shows how a user-created DLL is incorporated into WinCC in support entry 8301801, and the WinCC V8.0 scripting manual states that functions in existing DLLs can be enabled for functions and actions — both describe the flat-function path. To reach a VB component from a C-script, register it on the runtime PC and put a small C wrapper DLL between the script and the object. The sections below work one failure through the arithmetic and end with the decision for each case.
A VB ActiveX DLL and a Win32 DLL are not the same artifact #
A library you compile in C and export with __declspec(dllexport) carries your function names in its export table. Any process that can load the file finds those names and calls the functions, and nothing is registered anywhere. A Visual Basic ActiveX DLL is a different artifact: an in-process COM server that exposes classes, not functions. Its methods are reachable only through COM interfaces such as IDispatch, the registry maps the ProgID to the class ID and the file, and the class is instantiated with CoCreateInstance after the file has been registered. A Visual Basic EXE consumes one without effort because Visual Basic does the registry lookup and COM setup for you; the C-script engine does none of that, and that difference is the whole problem.
| Property | Flat Win32 DLL | VB ActiveX DLL |
|---|---|---|
| What the file exposes | named functions in the export table | classes, through COM interfaces |
| How a caller binds | name lookup in the export table | registry ProgID or CLSID, then CoCreateInstance |
| Registration | none | required on each PC that instantiates it |
| COM setup | none | CoInitialize before creation, CoUninitialize after release |
| Callable from a C-script that declares the name | yes | no, the method name is not an export |
What the ANSI-C script can actually reach #
WinCC's ANSI-C scripts call external code by declaring the function between #pragma code("Library.dll") and #pragma code(), and the runtime resolves each declared name against the export table of the DLL you name — the same lookup GetProcAddress performs. A VB ActiveX DLL's export table holds only the standard entry points a self-registering COM server needs. Your method name is not there, so the declared call fails before a single line of your Visual Basic code runs.
COM is the second gap. The script runtime performs no CoInitialize on your behalf, and the C subset available inside a script action gives you no practical way to run the full IDispatch sequence — class lookup, creation, GetIDsOfNames, DISPPARAMS setup, Invoke, cleanup — inline. The same declare-and-call mechanism, by contrast, is what lets a script reach operating system functions directly, as in calling Win32 mutex and semaphore functions in WinCC C-scripts. Flat exports: yes. COM objects: only through a wrapper.
Worked example: a GetProductionCount call that fails with class-not-registered #
The inputs: a Visual Basic 6 ActiveX DLL named ProductionLine.dll with one class whose ProgID is ProductionLine.Counters and one method GetProductionCount that returns a 32-bit integer (all three names are illustrative examples); a runtime PC on which that DLL has not been registered (the failing case); and the call chain a wrapper uses, in order — CoInitialize(NULL), CLSIDFromProgID on the ProgID, CoCreateInstance for IDispatch, GetIDsOfNames for the method name, then Invoke. That chain and its return codes are the documented COM sequence.
The rule in words: every call in that chain returns an HRESULT, a 32-bit value in which bit 31 — mask 0x80000000 — is the severity bit. Set means the call failed, clear means it succeeded, and the FAILED macro is nothing more than that one AND test.
The arithmetic, step by step:
- CoInitialize(NULL) returns S_OK,
0x00000000. Test:0x00000000AND0x80000000=0x00000000, clear, so COM is initialized. - CLSIDFromProgID on
ProductionLine.Countersreturns0x80040154, REGDB_E_CLASSNOTREG. Test:0x80040154AND0x80000000=0x80000000, set — the ProgID has no class registered under it, the chain stops here, and no Visual Basic code has run because there is no object to run it in. - Register the component from an administrator command prompt on the runtime PC, with the real path in place of the example path:
regsvr32 "C:\WinCC\Bin\ProductionLine.dll". Repeat the chain: CLSIDFromProgID returns S_OK —0x00000000AND0x80000000=0x00000000, clear — CoCreateInstance returns S_OK as well, GetIDsOfNames hands back the dispatch ID for the method name, and Invoke returns S_OK with the result variant typedVT_I4.
The result: the wrapper returns the count, and the script writes it to the tag.
The decision, with its threshold: the high bit is the whole test. Set on any HRESULT in the chain, and your Visual Basic method never executed — the fault is registration or COM setup on that machine, and no edit to the C-script can fix it. Clear through Invoke, and any remaining error is method-level: the DISP_E group in the list below.
The wrapper DLL that makes the call work #
Compile the wrapper as a plain Win32 DLL in the same bitness as the WinCC runtime process, link it against ole32.lib and oleaut32.lib, and export plain __stdcall functions. Every COM call stays inside the wrapper: initialize, create, invoke, release, uninitialize. CounterWrap, ProductionLine.Counters, GetProductionCount and the tag name below are illustrative; the functions they call are the documented COM API in ole32 and oleaut32.
/* CounterWrap.c - plain Win32 DLL, illustrative names */
#include <windows.h>
__declspec(dllexport) long __stdcall GetProductionCount(void)
{
IDispatch* pDisp = NULL;
CLSID clsid;
DISPID dispid;
OLECHAR* szMember = L"GetProductionCount";
DISPPARAMS dp = { NULL, NULL, 0, 0 };
VARIANT vResult;
HRESULT hr;
long lCount = -1;
if (FAILED(CoInitialize(NULL)))
return lCount;
do {
if (FAILED(CLSIDFromProgID(L"ProductionLine.Counters", &clsid)))
break;
if (FAILED(CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER,
&IID_IDispatch, (void**)&pDisp)))
break;
if (FAILED(pDisp->lpVtbl->GetIDsOfNames(pDisp, &IID_NULL,
&szMember, 1, LOCALE_USER_DEFAULT, &dispid)))
break;
VariantInit(&vResult);
if (SUCCEEDED(pDisp->lpVtbl->Invoke(pDisp, dispid, &IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dp, &vResult,
NULL, NULL)) && vResult.vt == VT_I4)
lCount = vResult.lVal;
VariantClear(&vResult);
pDisp->lpVtbl->Release(pDisp);
} while (0);
CoUninitialize();
return lCount;
}
A return of -1 means one step in the chain failed, and the HRESULT from that step is the diagnosis. In the script, the wrapper is declared and called like any flat DLL, and only the Visual Basic component is registered with regsvr32 — never the wrapper:
#pragma code("CounterWrap.dll")
long __stdcall GetProductionCount(void);
#pragma code()
long lCount;
lCount = GetProductionCount();
if (lCount >= 0)
SetTagDWord("ProductionCount", (DWORD)lCount);
Strings are the next complication once your real methods take text: a script hands you narrow char text while IDispatch wants BSTR, so the wrapper converts with MultiByteToWideChar plus SysAllocStringLen before Invoke, and a multi-parameter method takes its DISPPARAMS arguments in reverse order. Keep all of that inside the wrapper, where normal debuggers work. Change wrappers on a bench project first, and replace the DLL on the runtime PC only with the runtime stopped; a script that throws inside a configured action runs on every screen that calls it.
HRESULT codes worth knowing #
0x80040154REGDB_E_CLASSNOTREG — the class is not registered on that machine. Run regsvr32 as administrator on the runtime PC and check the ProgID spelling against the VB project.0x80020006DISP_E_UNKNOWNNAME — GetIDsOfNames did not recognize the member name. Compare it character for character with the method name in the component's type library.0x80020009DISP_E_EXCEPTION — the Visual Basic method itself raised an error. Invoke reported it faithfully; the fix lives in the VB class's own error handling, so test the DLL standalone.0x8001010ERPC_E_WRONG_THREAD — the object was created on a different apartment thread than the one now calling it. Initialize COM and create the object on the same thread.0x80070057E_INVALIDARG — argument count or type does not match the method signature. Recheck the DISPPARAMS count and the reverse argument order.
The decision for each case #
- A DLL you compiled in C with exported functions: declare it between the pragma code lines and call it directly. Nothing else is needed.
- A Visual Basic ActiveX DLL: register it on every PC whose scripts instantiate it, then call it only through a C wrapper. Treat the wrapper as a small permanent component, versioned alongside the project, not as a quick shim.
- No compiled wrapper possible in your project: move the logic into Visual Basic script, which creates registered COM objects itself through CreateObject with the ProgID. That engine has its own limits — what Visual Basic can and cannot do inside a WinCC project sets them out.
- Check the target before you plan, because panel targets carry different script engines and some have no C script at all: why a Comfort Panel shows only VB scripts, not C scripts covers the case that forces the VB route from the start.
Questions asked
Why does the same VB DLL work from a Visual Basic EXE?
A Visual Basic EXE initializes COM before any form or class code runs, so CoCreateInstance happens inside an already prepared apartment and your methods are reached through the component's interfaces. The WinCC C-script engine does none of that preparation: it resolves declared names against the export table only and leaves COM setup to the caller. That is why the component behaves perfectly in the VB test program and fails from the script.
Which DLL gets registered with regsvr32?
Only the Visual Basic component, because that is the COM server. The C wrapper is a plain DLL: it is never registered and only has to sit where the runtime process can load it. Running regsvr32 on the wrapper fails because it has no self-registration entry point, and skipping it on the VB DLL leaves the first COM call failing with the class-not-registered code.
Does bitness have to match between the DLLs and the runtime?
Yes. An in-process COM server loads into the calling process, so the Visual Basic component, the wrapper and the WinCC runtime must all match in bitness. A Visual Basic 6 ActiveX DLL is 32-bit only, which suits the 32-bit WinCC V7 runtime, while a 64-bit runtime process cannot load it at all; a mismatch shows up as the class failing to instantiate, not as a clear bitness message.
How are several parameters passed to the VB method?
When a method takes several parameters, the wrapper fills the COM argument array right to left: the method's last parameter sits at index 0. A Visual Basic method written as Total(a, b, c) is passed as c first, then b, then a, with the argument count set to 3. Getting the order wrong surfaces as the invalid-argument code rather than as a silently wrong result.