Giter VIP home page Giter VIP logo

Comments (2)

JeromySt avatar JeromySt commented on July 28, 2024 1

from dia-rs.

riverar avatar riverar commented on July 28, 2024

Hey @JeromySt, here's a sample. It's slightly inefficient; it allocates a 256 byte buffer up front, due to get_checksum not accepting a null pbdata. (This is a crate metadata issue, this method parameter isn't marked as [Optional]. I'll get this fixed.)

[package]
name = "sample_checksum"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies.windows]
version = "0.53"

[dependencies.microsoft-dia]
version = "0.10.0"
use microsoft_dia::{nsNone, DiaSource, IDiaDataSource, SymTagCompiland};
use windows::{
    core::*,
    Win32::System::Com::{CoInitializeEx, COINIT_MULTITHREADED},
};

fn main() -> Result<()> {
    unsafe {
        // Initialize COM and DIA
        CoInitializeEx(None, COINIT_MULTITHREADED).ok()?;
        let source: IDiaDataSource = microsoft_dia::helpers::NoRegCoCreate(
            s!(
                r#"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\DIA SDK\bin\amd64\msdia140.dll"#
            ),
            &DiaSource,
        )?;

        // Open session against own symbols
        let executable = std::env::current_exe().unwrap();
        source.loadDataForExe(&HSTRING::from(executable.as_os_str()), None, None)?;
        let session = source.openSession()?;

        // Get compilands
        let symbols =
            session
                .globalScope()?
                .findChildren(SymTagCompiland, None, nsNone.0 as u32)?;

        // Get source files
        for i in 0..symbols.Count()? {
            let symbol = symbols.Item(i as u32)?;
            let files = session.findFile(&symbol, PCWSTR::null(), nsNone.0 as u32)?;

            // Find files with a checksum and print out details
            for j in 0..files.Count()? {
                let file = files.Item(j as u32)?;
                if file.checksumType()? == 0 {
                    continue;
                }

                let mut byte_count = 0u32;
                let mut bytes = vec![0; 256];
                file.get_checksum(&mut byte_count, &mut bytes)?;
                bytes.truncate(byte_count as usize);

                println!("File: {}", file.fileName()?);
                println!(
                    "{:02x}: {}\n",
                    file.checksumType()?,
                    bytes
                        .iter()
                        .map(|b| format!("{:02x}", b).to_string())
                        .collect::<String>()
                );
            }
        }

        Ok(())
    }
}

from dia-rs.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.