By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

COM Interop

P: 4
I am a newbie to .NET (using 3.5) however have written a C# Class library that performs some decryption and need to be able to reference this class from Visual Basic 6.0.

I have attempted to do this using the following steps:

1. Set the assembly to "COM-Visible" from the project properties
2. Set "register for COM Interop" to tre in the "BUILD" properites of the project
3. Signed the assembly with a strong name
4. Built the assembly
5. Built the type library using the tlbexp tool
6. Registered the assembly using regasm tool
7. Registered the assembly in the GAC using gacutil

Although i need this to ultimately be called from VB6, our .NET Dev environment does not have VB6 so I am attempting to test the above using vbscript and receiving the following error "Active x component can't create object".

I have tried ensuring that the assembly and type library are present in the same directory as the wscript.exe file (although my understanding is that is not neccessary once the assembly has been installed in the GAC)

Any help would be much appreciated.




C# class:


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Security.Cryptography; //decryption class
  3. using System.IO;    //for use of memory stream 
  4.  
  5. namespace RBKDecrypt
  6. {
  7.     interface IDecrypt
  8.     {
  9.         string Key { set; }
  10.         string EncString { set; }
  11.         string DecryptString();
  12.     }
  13.  
  14.  
  15.     public class Decrypt:IDecrypt
  16.     {
  17.  
  18.         private string sKey;
  19.         private string sEncString;
  20.  
  21.  
  22.         public Decrypt()
  23.         {
  24.             //default constructor needed to access from COM
  25.         }
  26.  
  27.         public string Key
  28.         {
  29.             set{sKey = value;}
  30.         }
  31.         public string EncString
  32.         {
  33.             set { sEncString = value; }
  34.         }
  35.  
  36.  
  37.  
  38.  
  39.         public string DecryptString()
  40.         {
  41.  
  42.             // Create the Crypto Service Provider
  43.             DESCryptoServiceProvider desCrypto = new DESCryptoServiceProvider();
  44.  
  45.             // Set both the Key and the initialization vector to the key bytes
  46.             desCrypto.Key = HexStringToByteArray(sKey);
  47.             desCrypto.IV = HexStringToByteArray(sKey);
  48.  
  49.             // Decrypt the code
  50.             return DESDecrypt(HexStringToByteArray(sEncString), desCrypto);
  51.         }
  52.  
  53.  
  54.  
  55.         // Converts a HEX string to a byte array
  56.         public static byte[] HexStringToByteArray(String hex)
  57.         {
  58.             int NumberChars = hex.Length;
  59.             byte[] bytes = new byte[NumberChars / 2];
  60.             for (int i = 0; i < NumberChars; i += 2)
  61.                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  62.             return bytes;
  63.         }
  64.  
  65.         // Decrypt the byte array
  66.         public static string DESDecrypt(byte[] CypherText, SymmetricAlgorithm symmAlgorithm)
  67.         {
  68.             // Create a memory stream to the passed buffer.
  69.             MemoryStream ms = new MemoryStream(CypherText);
  70.  
  71.             // Create a CryptoStream using the memory stream and the CSP DES key.
  72.             CryptoStream encStream = new CryptoStream(ms, symmAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);
  73.  
  74.             // Create a StreamReader for reading the stream.
  75.             StreamReader sr = new StreamReader(encStream);
  76.  
  77.             // Read the stream as a string.
  78.             string val = sr.ReadLine();
  79.  
  80.             // Close the streams.
  81.             sr.Close();
  82.             encStream.Close();
  83.             ms.Close();
  84.  
  85.             return val;
  86.         }
  87.     }
  88. }
  89.  
vbscript:
Expand|Select|Wrap|Line Numbers
  1. Dim obj
  2. Set obj = CreateObject("RBKDecrypt.Decrypt")
  3. obj.Key="A818163DD5E0DE87"
  4. obj.EncString="FDA54C51E4BA3B0CB281CAF398F845BE"
  5. msgbox obj.DecryptString()
  6. Set obj = Nothing
  7.  
Dec 22 '08 #1
Share this Question
Share on Google+
2 Replies


Plater
Expert 5K+
P: 7,872
Hmm, is the DLL in the same directory, even after reigstering sometimes it needs to be there.

I have found when trying to create an activeX object of something in the GAC, I actually had to run the code through an IIS server. Although you're using vbscript so I don't know.
Dec 22 '08 #2

P: 61
@Plater
We recently had the similar issue ie we used VB6.0 activex component in C#. During the run, some times, we got the activex error(its client-server communication). But later, we found it was happening due to not releasing the used resources properly. Also, you may have to configure some other settings using dcomcnfg.
Dec 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.